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
Try these test cases
and
The first test case should be a smaller version of test case 8.
The second test case is an edge case which I think your code handles incorrectly.
Thank you so much qpwoeirut. Corrected the error and got it AC.