can someone point out what’s wrong with my code. logic seems to be right but no clue why it fails. not sure if there are any tricky corner case.
Did you add the condition to update H
only if the following condition holds true: 2(H-x) < H
?
Here’s an example:
Suppose H=5
, and at some point x=2
,
then it would not be prudent to update H
with 2(H-x)
because the new value of H will then become
H = 2 * (5 - 2) = 6
which is actually greater than the previous value of SLEEP REQUIRED. In that case, it would be best it we didn’t update the existing value of H
. Hope this solves your issue.
https://www.codechef.com/viewsolution/48302890
this one is a wrong solution but she got 100 points
it fails for
1
8 5
00001100
Hi dinesh! Iam getting WA. I think My approach is correct but couldn’t figure out the corner cases. Here are link:
https://www.codechef.com/viewsolution/48309267
https://www.codechef.com/viewsolution/48308660
Help with corner cases or wrong approach. Thank you!
Hey, I changed my condition to if(2(H-x)<H) H= 2*(H-X) and it worked but I don’t understand why. For example, my ‘x’ was stored in max-heap and hence, was largest possible value of x. If I skip this subtraction, the next value, say x’ is guaranteed to be smaller than x.
Plus, even if 2(H-x)>=H, the code will simply output “NO” in the end. So, why do we need this?
hey guys can anyone please tell me what’s wrong with my code?
#include
using namespace std;
#include
#define ff first
#define ss second
#define ll long long
#define pb push_back
#define w(x) int x; cin>>x; while(x–)
#define vi vector
#define pb push_back
int main() {
// your code goes here
w(x){
int l,h;
cin>>l>>h;
string s;
cin>>s;
int i=0;
int cz=0;
bool f=1;
while(i<l){
if(s[i]==‘0’){
cz++;
f=0;
}
else{
if(f==0){
h = 2*(h-cz);
cz=0;
}
//cout<<h<<endl;
}
i++;
}
h = 2*(h-cz);
if(h<=0){
cout<<"yes"<<endl;
}
else
cout<<"no"<<endl;
}
return 0;
}
1
11 4
1010001000
Give a try with this. I too missed one thing prior that chef can choose when to and not to sleep, he may not sleep at every free time.
1
11 4
1010001000
Your program gives no but its YES. And also have an eye on output format too like YES and Yes aren’t same except specified explicitly.
What I think you’re trying to say is that you’re taking the count of the highest continuous string of 0s and performing the operation.
Take this example
001000
First you would take 3, and then you would take 2 by your approach.
The problem is, he can’t go back in time. You have to take 2 first before 3 since it occurs first. Either that, or you skip the 2 and never take it.
Please help me ! what is wrong in my code?
#include <bits/stdc++.h>
#define ll long long int
#define debug(x) cout<<x<<"\n"
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
ll t;cin>>t;
while(t--){
ll n,h,count=0;
cin>>n>>h;
string s;
cin>>s;
for(ll i=0; i<n; i++){
if(s[i]=='0'){
count++;
}
else{
if(count >= h){
break;
}
else{
if(i != 0 && i!=n-1){
h = 2*(h-count);
count = 0;
}
}
}
}
if(count >= h){
cout<<"YES\n";
}
else{
cout<<"NO\n";
}
}
return 0;
}
That makes sense, thanks!
can you help me ? why my code give wrong answer?
#include <bits/stdc++.h>
#define ll long long int
#define debug(x) cout<<x<<"\n"
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
ll t;cin>>t;
while(t--){
ll n,h,count=0;
cin>>n>>h;
string s;
cin>>s;
for(ll i=0; i<n; i++){
if(s[i]=='0'){
count++;
}
else{
if(count >= h){
break;
}
else{
if(i != 0 && i!=n-1){
h = 2*(h-count);
count = 0;
}
}
}
}
if(count >= h){
cout<<"YES\n";
}
else{
cout<<"NO\n";
}
}
return 0;
}
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
bool solve()
{
long long h;
int l;
cin >> l >> h;
string s;
cin >> s;
bool answer = false;
int count = 0;
int i = 0;
while (i < l)
{
if (s[i] == ‘1’)
{
i++;
continue;
}
else if (s[i] == ‘0’)
{
count = 0;
while (i < l and s[i] == ‘0’)
{
i++;
count++;
}
//cout << count << " " << h << endl;
if (count < h)
{
h = 2 * (h - count);
//count = 0;
answer = false;
}
else if (count >= h)
{
//count = 0;
answer = true;
}
//cout << answer << endl;
}
}
//cout << answer << endl;
return count < h ? false : true;
}
int main()
{
ios_base :: sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t;
cin >> t;
while (t–)
{
if (solve())
cout << “Yes\n”;
else
cout << “No\n”;
}
return 0;
}
Can anyone help me to find the missing edge case?
how this if(prev>h) prev=h works?
try using h=min(h,2*(h-count));
i tried this question from my side i have all the cases runing from my side
it would be of great help if i know where am i failing.my solution
1
8 3
10100100
the mistake is that u should not let h to be increased;
use h=min(h,2*(h-count))
Thanks . I tried it works. Thank you for your precious time