Please read the new rule regarding the restriction on the use of AI tools. ×

Help in Problem 1066B

Revision en1, by khalnayak_ashu_007, 2020-04-27 00:50:22

Please help me with the problem 1066B - Heaters. I am searching the rightmost heater which should be switched on.

#define loop(i,a,b) for (int i = (a); i < (b); i++)

void solve(){
    int n,r;cin>>n>>r;
    vector<int> a(n);
    int j=-1,m_diff=-1;
    loop(i,0,n){
        cin>>a[i];
        if(a[i]==1){
            m_diff = max(i-j, m_diff);
            j = i;
        }
    }
    if((m_diff > (2*r-1)) || m_diff==-1)cout<<-1;
    else{
        int ans=0;
        int i=0, cur;
        while(i<n && i<=(r-1)){
            if(a[i]==1)cur=i;
            i++;

        }

        ans++;i=cur;
        while(i<n){
            i=cur+1;
            int i_cur=cur;
            while(i<n && i<=(2*r-1 +i_cur)){
                if(a[i]==1)cur=i;
                i++;
            }
            if(i>(i_cur+r-1) && i_cur !=cur)
                ans++;
        }
        cout<<ans;
    }
}

It's giving wrong answer on test 8. Submission 78237832

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en1 English khalnayak_ashu_007 2020-04-27 00:50:22 1029 Initial revision (published)