In line 54, u should push cnt0 if cnt0>0
try to debug then.
1
9 2
001000100
Is the answer 4 for that test case?
No, its 3. 
Weak test cases again then. My AC solution outputs: 4.
please can someone help me in off bulb problem i have tried all test cases which come to my mind please help
Here is my code
#include<bits/stdc++.h>
using namespace std;
//#define ll long long
#define F first
#define S second
#define N1 100005
#define N2 200005
#define endl “\n”
#define mod 1000000007
#define vll vector
#define pb push_back
#define vi vector
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL)
typedef long long ll;
int main()
{
fastio;
ll t;
cin>>t;
while(t–)
{
ll n,i,j,f=1,k,o=0,s=0;
cin>>n>>k;
string a;
cin>>a;
vectorv;
vectorstart(n),end(n);
vectorg(n);
ll x1=0,x2=0;
for(i=0;i<n;i++)
{
if(a[i]==‘0’)
{
x1++;
g[i]=1;
}
else
break;
}
for(j=n-1;j>=0;j–)
{
if(a[j]==‘0’&&g[j]==0)
{
x2++;
g[j]=1;
}
else
break;
}
ll ele=x1+x2;
for(i=0;i<n;i++)
{
if(a[i]==‘0’)
s++;
if(g[i]==1)
continue;
if(a[i]==‘0’)
{o++;}
else
{
if(o>0)
v.pb(o);
o=0;
}
}
//cout<<x1<<" “<<x2<<” “;
//sort(v.begin(),v.end());
if(x1>0&&x2>0)
{
v.pb(ele);
sort(v.rbegin(),v.rend());
//for(ll x : v)
//cout<<x<<” ";
ll count=0;
for(i=0;i<v.size();i++)
{
if(v[i]==ele)
count++;
}
for(i=0;i<v.size();i++)
{
if(k>=2)
{
s=s-v[i];
k=k-2;
if(v[i]==ele)
{
count--;
}
}
}
if(k>=1&&count>0)
{
if(x1>=x2)
{
s=s-x1;
}
else
{
s=s-x2;
}
}
}
else if(x1>0&&x2==0)
{
v.pb(x1);
sort(v.rbegin(),v.rend());
int f1=1;
//cout<<s<<" ";
ll count=0;
for(i=0;i<v.size();i++)
{
if(v[i]==x1)
count++;
}
for(i=0;i<v.size();i++)
{
if(k>=1&&v[i]==x1&&f1==1)
{
k=k-1;
count--;
s=s-v[i];
f1=0;
}
else if(k>=2)
{
k=k-2;
s=s-v[i];
// cout<<s<<" ";
}
// cout<<s<<" "<<k<<" ";
}
// cout<<s<<" ";
}
else if(x1==0&&x2>0)
{
v.pb(x2);
sort(v.rbegin(),v.rend());
int f1=1;
ll count=0;
for(i=0;i<v.size();i++)
{
if(v[i]==x2)
count++;
}
for(i=0;i<v.size();i++)
{
if(k>=1&&v[i]==x2&&f1==1)
{
k=k-1;
count--;
s=s-v[i];
f1=0;
}
else if(k>=2)
{
k=k-2;
s=s-v[i];
}
}
}
else
{
sort(v.rbegin(),v.rend());
for(i=0;i<v.size();i++)
{
if(k>=2)
{
s=s-v[i];
k=k-2;
}
}
}
cout<<s<<"\n";
}
}
no man should be 3
but it should be 3
codechef standards are deteriorating day by day. many like me would have wasted more then a hour on this question whose tastecase are so weak.
this condition can be handled ny the code in the later section.
how is it 3 ?
first we will remove first 2 bulb by cutting 2nd wire and with second last wire we will discoonect the last 2 bulb.
yes that give 10001. Then in order to disconnect every 0, you need 4 operations, no ?
I meant from this point 10001 you need to disconnect 4 times.
thanks
bro can u please clarify ur question.
The original string was 00000000 after performing 2 cuts 00/00000/00 we get this string. Then in the middle string we make any of them to be 1. Now our string becomes 00/11111/00, after this we start disconnecting our not satisfying 1’s those at index 4, 5, 6 considering it to 1 based indexing. so in order to disconnect 3 ones we need to disconnect 4 wires.
Is it like we are removing bulbs ? Not wires ? In disconnecting operation .
i agree
with given below test case, my code’s output is 3 but still I am getting wrong answer
1
9 2
001000100
from queue import PriorityQueue
t = int(input())
def disc(pq1, pq2, k, t1, t2):
# print(" >>> function call ", pq1.qsize(), pq2.qsize(), t1, t2)
deac, temp2 = 0, []
while k > 0 and (t1 != 0 or t2 != 0):
# print(t1, t2, k)
if t1 >= t2:
k -= 1
t1 -= -1pq1.get()
else:
if k - 2 >= 0:
k -= 2
t2 -= -1pq2.get()
else:
temp = -1pq2.get()
temp2.append(temp)
t2 -= temp
# print(" >>> after size", pq1.qsize(), pq2.qsize(), len(temp2))
while not pq1.empty():
deac += -1pq1.get()
while not pq2.empty():
deac += -1*pq2.get()
while len(temp2) != 0:
deac += temp2.pop()
return deac
for _ in range(t):
n, k = list(map(int, input().strip().split()))
s = str(input())
pq1 = PriorityQueue()
pq2 = PriorityQueue()
pq1_total, pq2_total, flag, zeros = 0, 0, 0, 0
for i in range(len(s)):
# print(flag, zeros, s[i])
if s[i] == ‘1’ and not flag:
flag = 1
pq1.put(-zeros)
pq1_total += zeros
zeros = 0
elif s[i] == ‘1’ and flag:
pq2.put(-zeros)
pq2_total += zeros
zeros = 0
else:
zeros += 1
if flag and zeros > 0:
pq1.put(-zeros)
pq1_total += zeros
zeros = 0
print(disc(pq1, pq2, k, pq1_total, pq2_total))`
Please help!