# SLPCYCLE - Editorial

Author: Daanish Mahajan
Tester: Istvan Nagy
Editorialist: Vichitr Gandas

SIMPLE

Greedy

# PROBLEM:

Given a body needs H units of continuous sleep. If you sleep x ( < H) units then next time, we need 2*(H-x) units of continuous sleep.
Given a string of length L describing the day where S_i=0 means that unit of time is free and S_i=1 means that unit of time is occupied. Find if you would be able to achieve the sleep requirement.

# EXPLANATION

As we know that if we sleep x(<H) units then we need to sleep 2*(H-x) units more next time.

So when we have some x(<H) units of free time available then should us sleep or not?
We should sleep only if, it reduces our remaining sleep units that is we should sleep x units only if 2*(H-x) < H.
=> 2*H - 2*x < H
=> 2*H - H < 2*x
=> H < 2*x
=> x > H/2

So iterate over the given string S, find count of continuous 0s, check if count > H/2, if yes then sleep for this time and update the H as following:
H = 2*(H-count)

If at any point we have H <= 0 then we are able to achieve the requirement otherwise no.

# TIME COMPLEXITY:

O(L) per test case

# SOLUTIONS:

Setter's Solution
#include<bits/stdc++.h>
using namespace std;
# define ll long long int

const int maxl = 1e5, maxh = 1e5, maxt = 10;

int main()
{
int t; cin >> t;
while(t--){
int l, h; cin >> l >> h;
string s; cin >> s;
int req = h; int cnt = 0;
for(int i = 0; i < l && req > 0; i++){
if(s[i] == '0')cnt++;
else{
if(cnt >= req / 2 + 1){
req -= cnt; req *= 2;
}
cnt = 0;
}
}
if(cnt >= req / 2 + 1 && req > 0){
req -= cnt; req *= 2;
}
string ans = req <= 0 ? "YeS" : "No";
cout << ans << endl;
}
}
Tester's Solution
#include <iostream>
#include <cassert>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
#include <random>

#ifdef HOME
#include <windows.h>
#endif

#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define forn(i, n) for (int i = 0; i < (int)(n); ++i)
#define for1(i, n) for (int i = 1; i <= (int)(n); ++i)
#define ford(i, n) for (int i = (int)(n) - 1; i >= 0; --i)
#define fore(i, a, b) for (int i = (int)(a); i <= (int)(b); ++i)

template<class T> bool umin(T &a, T b) { return a > b ? (a = b, true) : false; }
template<class T> bool umax(T &a, T b) { return a < b ? (a = b, true) : false; }

using namespace std;

long long readInt(long long l, long long r, char endd) {
long long x = 0;
int cnt = 0;
int fi = -1;
bool is_neg = false;
while (true) {
char g = getchar();
if (g == '-') {
assert(fi == -1);
is_neg = true;
continue;
}
if ('0' <= g && g <= '9') {
x *= 10;
x += g - '0';
if (cnt == 0) {
fi = g - '0';
}
cnt++;
assert(fi != 0 || cnt == 1);
assert(fi != 0 || is_neg == false);

assert(!(cnt > 19 || (cnt == 19 && fi > 1)));
}
else if (g == endd) {
assert(cnt > 0);
if (is_neg) {
x = -x;
}
assert(l <= x && x <= r);
return x;
}
else {
assert(false);
}
}
}

string readString(int l, int r, char endd) {
string ret = "";
int cnt = 0;
while (true) {
char g = getchar();
assert(g != -1);
if (g == endd) {
break;
}
cnt++;
ret += g;
}
assert(l <= cnt && cnt <= r);
return ret;
}
long long readIntSp(long long l, long long r) {
}
long long readIntLn(long long l, long long r) {
}
string readStringLn(int l, int r) {
}
string readStringSp(int l, int r) {
}

int main(int argc, char** argv)
{
#ifdef HOME
if(IsDebuggerPresent())
{
freopen("../in.txt", "rb", stdin);
freopen("../out.txt", "wb", stdout);
}
#endif
forn(tc, T)
{
int last = H;
int rem = H;
bool ok = false;
char pr = '1';
for (auto c : S)
{
assert(c == '0' || c == '1');
if (c == '1')
{
if (pr == '0')
{
rem <<= 1;
if (rem < last)
{
last = rem;
}
}
pr = c;
continue;
}
if (pr == '1')
{
rem = last;
}
pr = c;
--rem;
if (rem == 0)
ok = true;
}
if (ok)
printf("YES\n");
else
printf("NO\n");
}
assert(getchar() == -1);

return 0;
}

Editorialist's Solution
/*
* @author: vichitr
* @date: 26th June 2021
*/

#include <bits/stdc++.h>
using namespace std;

void solve() {
int H, L;
cin >> L >> H;
string S; cin >> S;
int cnt = 0;
for (char c : S) {
if (c == '0')
cnt++;
else {
if (cnt > H / 2) {
H = 2 * (H - cnt);
}
cnt = 0;
if(H < 0) break;
}
}
if (cnt > H / 2)
H = 2 * (H - cnt);
if (H <= 0) cout << "YES\n";
else cout << "NO\n";
}

int main() {

#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif

int t = 1;
cin >> t;
while (t--)
solve();
return 0;
}

# VIDEO EDITORIAL:

If you have other approaches or solutions, let’s discuss in comments.

3 Likes

Now, please tell me which test case is failing for my code.

Will be really appreciated.
Thanks.

1
7 5
0100000
The output of this testcase is “YES”.
But your code output is “NO”.

3 Likes

guys please say where i have mistaken
t=int(input())

for _ in range(t):

h,l=[int(a) for a in input().split()]

re=int(l)

s=input()

z=0

for i in s:

if i=="0":

z=z+1

else:

if z!=0:

if (re-z)!=0:

re=2*(re-z)

else:

re=re-z

z=0

if re==0:

break

if z!=0:

if (re-z)!=0:

re=2*(re-z)

else:

re=re-z

z=0

if re==0:

print("YES")

else:

print("NO")

How will the output be “YES”. He 1st sleeps 1 unit so he has to sleep another 2*(5-1)=8 units but the remaining zeros are 5.

#include <bits/stdc++.h>
using namespace std;

int main()
{
int t;
cin>>t;
while(t–)
{
int l,h;
cin>>l>>h;
string s;
cin>>s;

map<int,int> mp;

//storing what all contiguous unit of time is available
for(int i=0;i<l;i++)
{
int c=0;
while(i<l && s[i]=='0')
{
c++; i++;
}
if(c!=0)
mp[c]++;
}

bool ans=false;
while(mp.empty()==false)
{

auto it=mp.lower_bound(h);
if(it==mp.end())
it--;

h=2*(h-((*it).first));
((*it).second)--;

if(((*it).second)==0)
mp.erase((*it).first);

if(h<=0)
{
ans=true;
break;
}

}

if(ans==true) cout<<"Yes\n";
else cout<<"No\n";
}
return 0;

}

check with edge cases
https://www.codechef.com/viewsolution/48288067

didn’t consider the fact that he may have the option of not sleeping during free time. UGHH. But it wasn’t mentioned clearly ;-;

9 Likes

But what happens when he does not sleeps 1 unit and work for 1 unit and sleep afterward?

1 Like

You are computing H = 2 ( H - X ) every time x is less than H. That will give you a wrong answer. What you should do is :
if 2(H-X) < H
H = H-X

1 Like

what is wrong with my code?suggest some corner cases.

#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define mod 1000000007

void solve()
{
ll L,H;
cin>>L>>H;
string s;
cin>>s;
int flag=0;
ll count=0;

ll i=0;
for( i=0;i<L;i++)
{
if(s[i]=='1')
{
if(flag==1)
{
H=2*(H-count);
// if(H==0)
// break;
flag=0;
}
count=0;
}
if(s[i]=='0' )
{
count++;
flag=1;
}

}
if(i==L && s[L-1]=='0')
{
H=H-count;
}

if(H>0)
cout<<"NO"<<endl;
else if(H<=0)
cout<<"YES"<<endl;
}

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

ll T;
cin>>T;
while(T--)
{
solve();
}
}

I think quite differently -

1.push all the length of continuous zeroes in a vector (denotes that how much my user can sleep continuously)
2.Now if there are no zero answers is NO
3. else, let prev = h denotes we need this hrs to sleep, now iterate my vector, if my current value (which is basically the length of continuous zeroes ) is less than prev, then I will change my previous to prev = 2 * (prev-vc[i]); (as question said)

Also, one thing is to check sometimes while doing this (prev = 2 * (prev-vc[i]); ) , my prev is greater than h , so now we will set prev to h because from now we will check from that time frame no need to take previously.

otherwise(if vc[i]>prev) will set flag=1 means we get enough continuous hrs to sleep.

in last what I add is to check max continuous zeroes, we r checking here from starting and doing all the operations above may increase my prev, so I directly check whether the max continuous zeroes are greater than equal to h or not, if yes then I will simply sleep in that time frame only.

lli n,h;
cin>>n>>h;
string s;
cin>>s;
lli cnt=0;
vi vc;
loop(i,n)
{
if(s[i]=='0')
cnt++;
else
{
if(cnt)
vc.pb(cnt);
cnt=0;
}
}
if(cnt)
vc.pb(cnt);
if(sz(vc)==0)
{
print("NO");
}
else
{
lli prev = h;
lli flag=0;
loop(i,sz(vc))
{
if(vc[i]<prev)
{
prev = 2*(prev-vc[i]);
if(prev>h)
prev = h;
}
else
flag=1;
}
if(flag==1 or h<=(*max_element(all(vc))))
print("YES");
else
print("NO");
}

}
return 0;
}

solution : CodeChef: Practical coding for everyone

1 Like

Same with me too…

Can anyone point out error in my code. I’m unable to understand where is my code failing?

#include
#define ll long long
using namespace std;

int main() {
ll t,h,l;
cin>>t;
while(t–){
cin>>l>>h;
string s;
cin>>s;
ll count=0,flag=0;
if(h==0){
cout<<“YES”<<endl;
continue;}
for(ll i=0;i<l;i++){
if(s[i]==‘0’)
count++;
else{
if(h<=count){
flag=1;
break;}
if(count){
h=2*(h-count);}
count=0;
}}
if(h<=count || flag==1)
cout<<“YES”<<endl;
else
cout<<“NO”<<endl;
}
return 0;
}

can someone give a test case which my code might fail to pass because I am still trying but cannot find a test case on which I can improve my code? Any help will be recommended thanks:

My WA code link : CodeChef: Practical coding for everyone

PS: Ignore bad variable names, sorry for that

how will it be YES?

#include<bits/stdc++.h>
#define ll long long
using namespace std;
// ios_base :: sync_with_stdio(false);
// cin.tie(NULL);

//////////////////////// Sleep Cycle ///////////////////
void solve() {
int l , h;
string s;
cin >> l >> h;
cin >> s;
int req = h;
int count = 0;
for(ll i = 0 ; i < s.length() ;) {
if(s[i] == '0') {
while((s[i] == '0') && (i < s.length())) {
count ++;
i++;
}
if(count >= req) {
cout << "YES" << endl;
return;
} else {
req = 2 * (req-count);
count = 0;
}
} else {
i++;
}

}
cout << "NO" << endl;
}

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

int tc;
cin >> tc;
while(tc--) {
solve();
}
}

Can anyone tell me what is wrong here ? I passed the sample cases but got WA after submitting.

# include

using namespace std;
int n,h;
int t;
int a=0;

int main()
{
cin>>t;
while(t–){
cin>>n>>h;
char str[n] ;
cin>>str;
int count=0;
for(int i=0;i<n;i++){
// if(str[i]==‘0’){
// int j=i;
count=0;
while(str[i]==‘0’ && i<n){
count++;
if(count==h){
a=1;
break;
}
i++;

}
if(count!=0){
h=2*(h-count);

}

}
if(a==0){
cout<<“NO”<<endl;
}
else{
cout<<“YES”<<endl;
}

}

return 0;

}
where am I wrong??

https://www.codechef.com/viewsolution/48269643

please can you tell what’s wrong with my code?

Now that I realized my mistake, or rather lack of observation: 2(H-x) < H, I feel stupid.

1 Like