Shikharraj's blog

By Shikharraj, history, 2 hours ago, In English

Hello CF community,

I am stuck on problem (Cost of the Array). My solution is getting wrong answer on test case 2 and I am not sure why.

My submission : 304400545

Any counterexample for the above solution?

Thanks.

  • Vote: I like it
  • +3
  • Vote: I do not like it

»
2 hours ago, # |
  Vote: I like it 0 Vote: I do not like it
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define vl vector<ll>
#define vi vector<int>
#define vvl vector<vector<ll>>
#define vvi vector<vector<int>>
#define vvpll vector<vector<pair<ll, ll>>>
#define vb vector<bool>
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(v) v.begin(), v.end()

void solve()
{
    ll n, k;
    cin >> n >> k;
    vl a(n);
    for (int i = 0; i < n; i++)
    {
        cin>>a[i];
    }
    ll ans = k / 2 + 1;
    ll j = k;
    ll x = 1;
    int i;
    for (i = n - 1; i >= 0; i--)
    {
        if(j > 2){
            j--;
            continue;
        }else{
            x = a[i];
            i--;
            break;
        }
    }
    if(x != 1){
        ans = min(ans, 1LL);
    }else if(x == 1 && i >= 1){
        ans = min(ans, 2LL);
        if(a[i] != 1){
            ans = min(ans, 1LL);
        }
    }
    cout<<ans<<endl;
}

int main()
{
    ll tt;
    cin >> tt;
    while (tt--)
    {
        solve();
    }
    return 0;
}