PROBLEM LINK:
Practice
Contest: Division 3
Contest: Division 2
Contest: Division 1
Author: Daanish Mahajan
Tester: Istvan Nagy
Editorialist: Vichitr Gandas
DIFFICULTY:
SIMPLE
PREREQUISITES:
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) {
return readInt(l, r, ' ');
}
long long readIntLn(long long l, long long r) {
return readInt(l, r, '\n');
}
string readStringLn(int l, int r) {
return readString(l, r, '\n');
}
string readStringSp(int l, int r) {
return readString(l, r, ' ');
}
int main(int argc, char** argv)
{
#ifdef HOME
if(IsDebuggerPresent())
{
freopen("../in.txt", "rb", stdin);
freopen("../out.txt", "wb", stdout);
}
#endif
int T = readIntLn(1, 10);
forn(tc, T)
{
int L = readIntSp(1, 100'000);
int H = readIntLn(1, 100'000);
string S = readStringLn(L, L);
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.