BULBS - Editorial

Please help finding the bug. Here’s the commented code
PS- Thanks in advance

#include<bits/stdc++.h>
//#include <boost/multiprecision/cpp_int.hpp>
//using namespace boost::multiprecision;
#define speed ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
//#pragma GCC optimize(“Ofast”)
#pragma GCC target(“avx,avx2,fma”)
#pragma GCC optimization (“unroll-loops”
#pragma GCC optimize “trapv”
#define _GLIBCXX_DEBUG
#define ll long long int
#define ld long double
#define ull unsigned long long int // ranges from (0 - twice of long long int)
#define rep(i,a,n) for (ll i=a;i<n;i++)
#define per(i,a,n) for (ll i=n-1;i>=a;i–)
#define pb push_back
#define mp make_pair
#define vll vector
#define MOD 1000000007LL
#define llpair pair<ll,ll>
#define INF 1000000000000000000ll // this is 1e18 long long
#define np next_permutation
#define PI acos(-1)
#define DEB(x) cout<<#x<<" "<<x<<endl;
#define rotate_left(vec,amt) rotate(vec.begin(),vec.begin()+amt,vec.end());
#define rotate_right(vec,amt) rotate(vec.begin(),vec.begin()+vec.size()-amt,vec.end());
#define all(x) x.begin(),x.end()
#define sortall(x) sort(all(x))
#define clr(x) memset(x,0,sizeof(x))
using namespace std;
int main()
{
speed;
ll t=1;
cin>>t;
while(t–)
{
ll n,k;
cin>>n>>k;
string s;
cin>>s;
vectorone,two; // one stores the length of 0s block which reuire 1 cut
// similarly for two

    ll start=-1,finish=n,cnt=0;         // start - index for first occurence of 1 
                                        // finish - index for occurence of 1 from last
    rep(i,0,n)
    cnt+=(s[i]=='0');                   // count no of 0

    if(cnt==n)
    {
        cout<<0<<endl;
        continue;

    }
    rep(i,0,n)
    {
        while(s[i]=='0')
        {
            i++;                        // finding the index and also the length of block of 0 from starting
            start=i;
        }
        break;  
    }

    per(i,0,n)
    {
        while(s[i]=='0')                //// finding the index and also the length of block of 0 from starting
        {
            i--;
            finish=i;
        }
        
        break;
    }

    if(start!=-1)
    one.pb(start);                      // storing in one 
    
    if(finish!=n)
    one.pb(n-finish-1);                 // storing in one
    else                        
    {
        finish--;
    }

    sort(one.begin(),one.end(),greater<ll>());          // sort in descending

    for(ll i=start;i<=finish;i++)
    {
        ll len=0;
        while( i<=finish && s[i]=='0')                   // finding blocks of 0 which require 2 cuts
        {
            i++;
            len++;
        }
        // DEB(len);
        if(len!=0)
        two.pb(len);
    }
    sort(two.begin(),two.end(),greater<ll>());          //sort in desc
    
    if(k==0)
    {
        cout<<cnt<<endl;                // if k==0
    }

    ll ans=0,size=two.size();           // ans stores the maximum zeroes that can be removed using cuts

    if(one.size()>0 && k>0)
    {
        ll temp=one[0];                // in each if block finding max ans for it
        ll also=k-1;                    
        rep(i,0,min(size,also/2))       // also stores the updated value of k 
        {                               // we can take atmost max(two.size(),also/2) elements
            temp+=two[i];
        }
        ans=max(ans,temp);
    }
    if (one.size()>1 && k>1 )
    {
        ll temp=one[0]+one[1];
        ll also=k-2;
        rep(i,0,min(size,also/2))          // similar as above for scenario when k>1 and one.size()>1
        {
            temp+=two[i];
        }
        ans=max(ans,temp);
    }

        if(k>=2) 
        {                                   // finally checking if we can obtain the max value by removing only 2 cut requiring elements
        ll t1=0;
        rep(i,0,min(size,k/2))
        {
            t1+=two[i];
        }
        ans=max(ans,t1);
        }
        cnt-=min(cnt,ans);
    cout<<cnt<<endl;

}
return 0;

}

weak testcases
many of the solutions got accepted on

1
12 2
000100001000

correct output : 4
those who got 6 as output, got accepted.

1 Like

Weak test cases ruined my efforts…very disappointed by the testers

3 Likes

I’m getting the output 3 but I’m still getting WA on submissionhttps://www.codechef.com/viewsolution/38085094. Please help in finding the problem.

I’m also getting answer 4 but still I’m getting WA on submission.CodeChef: Practical coding for everyone

Just make a vector of partial blocks and whole blocks. sort descending.
Then ans1 = we fix partial blocks first by using 1 moves (k–)
ans2 = we fix whole blocks first by using 2 moves(k-2)
Also if there are two partial blocks(there can be 2 at max), then,
ans3 = we fix only one of the partial blocks and then the whole blocks and then the second partial block.
ans is the minimum of the three.
My submission: CodeChef: Practical coding for everyone

Why am i getting wrong answer i implemented same approach??
My solution- CodeChef: Practical coding for everyone

https://www.codechef.com/viewsolution/38086602 Can you tell me the error in this code.

@rishup_nitdgp @manishtanwar @rahuldugar @admin

Hidden test cases of the codechef september 20 cook off were very weak.
On problem CodeChef: Practical coding for everyone.
if the test case is
1
16 2
0010000100001000

Then most of the accepted submissions gives the output 9.
But its correct output should be 8.
We need to cut (2-3) and (12-13) wire and then disconnect 4, 5, 6, 7, 9, 10, 11, 12 bulbs.

One of the submissions that was incorrect and got accepted is CodeChef: Practical coding for everyone.

This was the random person i don’t know.

I was stucked in fixing this test case and my cook off got ruined.

How can this much important test case can be missed by the setters.

This contest should be unrated for all.

3 Likes

Finally got AC.

here is my accepted solution.

1 Like

congo :wink:

https://discuss.codechef.com/t/weak-hidden-test-cases-of-cook-off-september/78103/15

Please help still getting WA. I got the correct output for all the above-hidden test cases.
https://www.codechef.com/viewsolution/38094884

here is my solution
i have tries all possible test cases
and all of them were correct.
but still i got wrong answer.
please help me!
i am very tensed.

Can anyone check what is wrong in this solution
https://www.codechef.com/viewsolution/38098293

Thanks a lot… I was wondering where my solution was going wrong :slight_smile:

2 Likes

Can someone tell me where did i go wrong?

#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
typedef long long int ll;
// Declare a multimap
multimap<int, string> MM;

define M_PI 3.14159265358979323846

const int M=1e9+7;
long long mod(long long x){
return ((x%M + M)%M);
}
long long add(long long a, long long b){
return mod(mod(a)+mod(b));
}
long long mul(long long a, long long b){
return mod(mod(a)*mod(b));
}

int main()
{
ll t;
cin>>t;
while(t–)
{
ll n,k,m,l,r,c=0,i,j,fs=0,vs=-1,ve=-1;
cin>>n>>k;
string s;
ll z[n];
cin>>s;
j=0;
for(i=0;i<n;i++)
{
if(s[i]==‘0’ && i==n-1)
{
c++;
ve=c;
}
else if(s[i]==‘0’)
{
c++;
if(i==0)
fs=1;
}
else if(s[i]==‘1’ && c>0)
{
if(fs==1)
{
fs=0;
vs=c;
}
else
{
z[j]=c;
j++;
}
c=0;
}
//cout<<c<<" “;
}
//cout<<”\n";
sort(z,z+j,greater());
c=0;
//cout<<vs<<" “<<ve<<”\n";
for(i=0;i<j;i++)
{
if(k>=2)
{
if(vs!=-1 && ve!=-1 && (vs+ve)>z[i])
{
i–;
k=k-2;
vs=-1;
ve=-1;
}
else if(vs!=-1 && ve!=-1 && max(vs,ve)>z[i])
{
i–;
k–;
if(vs>ve)
vs=-1;
else if(ve>vs)
ve=-1;
}
else if(vs!=-1 && vs>z[i])
{
i–;
k–;
vs=-1;
}
else if(ve!=-1 && ve>z[i])
{
i–;
k–;
ve=-1;
}
else
{
k=k-2;
}
}
else
{
c=c+z[i];
}
//cout<<z[i]<<"("<<k<<") , ";
}

    while(k>0)
    {
        if(vs!=-1 && ve!=-1)
        {
            k--;
            if(vs>ve)
                vs=-1;
            else 
                ve=-1;
        }
        else if(vs!=-1)
        {
            k--;
            vs=-1;
        }
        else if(ve!=-1)
        {
            k--;
            ve=-1;
        }
        if(vs==-1 && ve==-1)
            break;
    }
    if(ve!=-1)
        c=c+ve;
    if(vs!=-1)
        c=c+vs;
    cout<<c<<"\n";
}
return 0;

}

//in name of THE GOD
#include
#include
#include <string.h>
#include<bits/stdc++.h>
#define ll long long int
#define pb push_back
#define tinput int t; cin>>t; while(t–)
#define rep(i,n) for((i)=0; (i)<(n); (i)++)
ll min(ll x, ll y) {
return (x<y)?x:y;
}

int max(int x, int y) {
return (x>y)?x:y;
}
// atoi string convert into int
// stol string convert into long
// stoll string convert into long long int
// memset(dp, -1, sizeof(dp));
// cout<<fixed<<setprecision(10)<<ans; for 10 decimal value

using namespace std;

int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);

// READ THE QUESTION PROPERLY
tinput
{
int n,k,x,y,count = 0; cin>>n>>k;
string s;
cin>>s;
vectorzmid,zend,gcut;

for(int i=0;i<s.size();i++) if(s[i] == '1') count++;

if(count == 0 || count == n)
{
   cout<<0<<"\n";
}
else if(k == 0)
{
   cout<<n-count<<"\n";
}
else
{

for(int i=0;i<s.size();i++)
{
if(s[i] == ‘1’)
{
zend.pb(i);
x=i;
break;
}
}
for(int i=s.size()-1;i>=0;i–)
{
if(s[i] == ‘1’)
{
zend.pb(s.size()-1-i);
y=i;
break;
}
}

/int z=x+1;
while(z!=y+1)
{
if(s[z] == ‘1’ && z-x-1!=0)
{
zmid.pb(z-x-1);
x=z;
}
else if(s[z] == ‘1’ && z-x-1 == 0) x=z;
z++;
}
/

//cout<<x<<" “<<y<<”\n";

int fp=x,sp=x+1;
while(sp!=y+1)
{
if(s[sp]==‘1’)
{
if(sp-fp-1 == 0)
{
fp=sp;
}
else
{
int zerocount = sp-fp-1;
zmid.pb(zerocount);
fp=sp;
// cout<<zerocount<<"\n";
}
}
sp++;
}

sort(zmid.begin(),zmid.end(),greater());
sort(zend.begin(),zend.end(),greater());

/* for(int i=0;i<zmid.size();i++) cout<<zmid[i]<<" “;
cout<<”\n";

for(int i=0;i<zend.size();i++) cout<<zend[i]<<" “;
cout<<”\n";*/

if(k%2!=0)
{
k–;
gcut.pb(zend[0]);
zend[0]=zend[1];
zend[1] = 0;
}

int i=0;
while(k!=2 && i!=zmid.size())
{
gcut.pb(zmid[i]);
k-=2;
i++;
}

if(k==2 && i!=zmid.size())
{
if(zmid[i] > zend[0]+zend[1])
{
gcut.pb(zmid[i]);
k-=2;
}
else
{
gcut.pb(zend[0]);
gcut.pb(zend[1]);
}
}
else if(k == 2 && i == zmid.size())
{
gcut.pb(zend[0]);
gcut.pb(zend[1]);
}
else if(k!=2 && i==zmid.size())
{
gcut.pb(zend[0]);
gcut.pb(zend[1]);
}

//for(int a=0;a<gcut.size();a++) cout<<gcut[a]<<" “;
// cout<<”\n";

int sum = 0;
for(int a=0;a<gcut.size();a++)
sum+=gcut[a];

cout<<n-sum-count<<"\n";
}
}

return 0;
}

can anyone tell what wrong with this code, I got right ans for all test cases given above,
still gives wrong ans.