MAXVAC - Editorial

Hi Ashutosh,

I have also calculated countZerosBeforeOne & countZerosAfterOne , and then checking whether I’ll get extra vacation if I flip that bit, if “Yes” then I’m keeping it else moving. Also updating “countZerosBeforeOne = countZerosAfterOne”, but only 1st and 4th task is getting passed and not the 2nd and 3rd one.

I also want to know the test case for which it fails. If you have solved it correctly then do let me know. Although I can check other’s solution but I have really put everything in it and want to know where I’m doing wrong.

can anyone say what is wrong my my code please?
#include<bits/stdc++.h>
#define int long long
#define vi vector
#define mod 1000000007
#define ii pair<int,int>
#define vii vector
#define pb push_back
#define ff first
#define ss second
#define fill(a,b) memset(a,b,sizeof(a))
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define F0R(i,n) for (int i=0; i<(n); i++)
#define all(n) n.begin(),n.end()
#define allr(n) n.rbegin(),n.rend()
#define INF 1e18
using namespace std;

int32_t main(){
IOS;
int t;cin>>t;
while(t–){
int n,x;cin>>n>>x;
string s;cin>>s;
int ar[n];
int cnt=0;
for(int i=0;i<x;i++){
if(s[i]==‘1’)++cnt;
}
int l=0;
ar[l]=cnt;
for(int r=x;r<n;r++){
if(s[l]==‘1’)–cnt;
if(s[r]==‘1’)++cnt;
++l;
ar[l]=cnt;
}
int ans=0;
bool used = false;
int i=0;
while(i < n-x+1){
if(ar[i]==0){
++ans;
i += x;
}
else if(ar[i]==1 && !used){
++ans;
i += x;
used = true;
}
else{
++i;
}
}
cout<<ans<<’\n’;
}
}

yes! but it is giving the output same output 2;

Infact I implemented the same idea,
but, I am not able to figure out the cases that are failing…,
DO any one try…

void solve()
{
int n,x;
cin>>n>>x;
string s;

cin>>s;
int i=0;

bool flag=true;
int ans=0;
while(i+x<=n){
	string t=s.substr(i,x);
	int count=0;
	for (int j= 0; j < x; ++j)
	{
		if(t[j]=='0'){
			count++;
		}
	}
        
	if(count==x-1 && flag){
		
		ans++;
		flag=false;
		i=i+x;
		
	}

	
	else if(x==count){
		//cout<<"i"<<i<<"\n";
          ans++;
          i=i+x;

	}
	else
	i++;
}
cout<<ans<<"\n";

}

Hey ! can anybody tell me a test case where this is failing ???

import java.util.;
import java.lang.
;
import java.io.*;

/* Name of the class has to be “Main” only if the class is public. */
class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
while(t–!=0){
int n=sc.nextInt();
int x=sc.nextInt();
String s=sc.next();
int zero=0;
int ans=0;
int one=1;
for(int i=0;i<n;i++){
if(zero==x){
ans++;
zero=0;
}
char ch=s.charAt(i);
if(ch-‘0’==0){
zero++;

	    }
	    else if(one==1){
	        
	        
	            if(zero+1==x){
	                one=0;
	                ans++;
	                zero=0;
	                 
	            }
	            else{
	                 int j=i+1;
	                 int zero1=zero+1;
	                while(j<n && zero1!=x){
	                    ch=s.charAt(j);
	                    if(ch-'0'==1){
	                       break; 
	                    }
	                    else{
	                        zero1++;
	                    }
	                    j++;
	                }
	                if(zero1==x){
	                    ans++;
	                    zero=0;
	                    i=j-1;
	                    one=0;
	                  //   System.out.println(ans+" "+i);
	                }
	                else{
	                    zero=0;
	                }
	            }
	        
	         
	    }
	    else
	    zero=0;
	    
	    
	}
	 System.out.println(ans);
	    
	}
}

}

can you share the whole code in quotes so that it’ll be easy to tell you the test case.

I wrote all the common mistakes done in this problem . Might help :sneezing_face:

@uditkumarjain @boomer_coder I will try to answer both things.

So, the dp stores two states: prefix and exact count of flips (which could be 0/1 acc to problem). Now, the transition is, (after i >= x, because when i < x, I can’t go on holiday)

  • If we have 0 at current index i, then we can’t do any flips here, so either we can have flips earlier in range i - x or we can’t.
  • If we have 1 at i, then we can flip it, and add the result of i - x to it, and if we’ll not flip it, then result is same as dp[i-1].
1 Like

Binary search on number of vacations , if we can do 3 vacations so we can have 1, 2 as well.

MY code link : code link

11 4
00100010000
7 1
1111100

your answer : 1 2
correct answer : 2 3

Not sure which test case this fails under. Passed all of them within this editorial. A non-passing test-case would be appreciated : )

#include<bits/stdc++.h>
using namespace std;
void solve();
int main()
{

#ifndef ONLINE_JUDGE
	// for getting input from input.txt
	freopen("input.txt", "r", stdin);
	// for writing output to output.txt
	freopen("output.txt", "w", stdout);
#endif

	int t;
	cin >> t;
	while (t--)
	{
		solve();
		cout << "\n";
	}

	cerr << "time taken : " << (float)clock() / CLOCKS_PER_SEC << " secs" << endl;
	return 0;
}
void solve()
{
	int n , x;
	cin >> n >> x;
	string s;
	cin >> s;
	int prev_max = 0, curr = 0, prev_id = 1;
	bool flag = 0;
	for (int i = 0; i < n; i++) {
		if (s[i] == '1') {
			if (!flag) {
				curr++;
				flag = 1;
				prev_id = i;
			}
			else {
				prev_max = max(prev_max / x, curr / x) * x;
				flag = 0;
				i = prev_id;
				curr = 0;
			}
		}
		else {
			curr++;
		}
		prev_max = max(prev_max / x, curr / x) * x;

	}
	cout << prev_max / x;
}

Someone give this man an award!

https://www.codechef.com/viewsolution/57231879
but now this test case working but still i m getting wrong answer

can someone please find, error in my code ?
here is my code:-
https://www.codechef.com/viewsolution/57193498

Can anyone tell me a test case where this code fails??

/* package codechef; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Main
{
    public static void solve(String s,int n,int k){
        int arr[] = new int[n];
        int ext[] = new int[n];
        int i=n-1;
        int ans=0;
        int count=0;
        while(i>=0){
            if(s.charAt(i)=='0'){
                count++;
                if(count==k){
                    ans=ans+1;
                    arr[i]=ans;
                    count=0;
                }else{
                    arr[i]=ans;
                }
            }else{
                count=0;
                arr[i]=ans;
            }
            ext[i]=count;
            i--;
        }
        count=0;
        i=0;
        while(i<n){
            if(s.charAt(i)=='0'){
                count++;
                if(count==k){
                    count=0;
                }
            }else{
                int t=count+1;
                if(t==k){
                    System.out.println(arr[0]+1);
                    return;
                }
                if((i+1) < n && s.charAt(i+1)=='0'){
                    t+=ext[i+1];
                    if(t==k){
                       System.out.println(arr[0]+1);
                       return; 
                    }
                }
                count=0;
            }
            i++;
        }
        System.out.println(arr[0]);
    }
	public static void main (String[] args) throws java.lang.Exception
	{
		// your code goes here
		Scanner sc= new Scanner(System.in);
		int t=sc.nextInt();
		while(t-->0){
		    int n=sc.nextInt();
		    int k=sc.nextInt();
		    String s=sc.next();
		    solve(s,n,k);
		}
	}
}

This can be solved using a simple greedy approach
Count all gaps of ones and zeroes amd store them in a vector
Then iterate over all gaps of ones and check check whether including extra zero in adjacent zeroes gap can increase number of vacations or not .
In case if there is only one 1 between 2 gaps of zeroes merge them and check the same

My solution link
https://www.codechef.com/viewsolution/57294870

Good Afternoon sir,
My name is Kunal jain,I am new to coding,I tried to do this maxvac question.
I also didi the code but it is not submitting,it is showing that my two test cases are failing ,So can you please help me to find out my which test cases are failing?
link to my code is this-
https://www.codechef.com/viewsolution/57981526

What’s wrong in my answer

from audioop import reverse

b = 0
index = 0
t = int(input())
for i in range(1,t+1):
n,w = input().split()
n = int(n)
w = int(w)
a = list(map(int, input().split()))
a.sort(reverse=True)
print(a)
for indexs in a:
index += 1
b += indexs
if b > w:
print(n-index)
break
if indexs == a[-1] and b <= w :
print(‘0’)
break
b = 0
indexs = 0
index = 0

Hey can you please provide your code along with proper indentation, as it is pretty confusing what a python code is exactly intended to do without indentation

You’re in the wrong topic @nishu_real ; here is the HOLIDAYS editorial

As a general hint though, you can preserve formatting by adding a line that starts with three back ticks ``` before and after your code.

As a specific observation, you don’t need the audioop library for any problem on this site that I know of. The sort/sorted built-ins take reverse as a parameter anyway.