Please read the new rule regarding the restriction on the use of AI tools. ×

One gets accepted other don't, even-though they seem similar.

Revision en1, by Rishabh.Jain, 2020-10-08 16:50:17

Cards

This code got accepted ~~~~~

include <bits/stdc++.h>

using namespace std; typedef long long int lli;

define int lli

int n; int m; vector arr; vector inp; void can(int pos){ if(pos == m){ cout<<"YES"<<endl; exit(0); }

int cur = inp[pos]; assert(cur>=0);

if(arr[cur]<=0){ return; }

arr[cur]--;

if(cur == 0){ if(arr[cur+1]>0){ arr[cur+1]--; can(pos+1); arr[cur+1]++; } }else{ if(arr[cur-1]>0){ arr[cur-1]--; can(pos+1); arr[cur-1]++; } if(arr[cur+1]>0){ arr[cur+1]--; can(pos+1); arr[cur+1]++; }

} } void solve(){ cin>>n>>m; arr.resize(n+4); inp.resize(m); for(int i=1;i<=n;i++){ arr[i-1]++; arr[i]++; }

for(int i=0;i<m;i++){ cin>>inp[i]; } sort(inp.begin(), inp.end()); can(0);

cout<<"NO"<<endl; } signed main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); solve(); }


But the following code, which is essentially the same(looks same to me) didn't:

include <bits/stdc++.h>

using namespace std; typedef long long int lli;

define int lli

int n; int m; vector arr; vector inp; void can(int pos){ if(pos == m){ cout<<"YES"<<endl; exit(0); }

int cur = inp[pos]; assert(cur>=0);

if(arr[cur]<=0){ return; }

arr[cur]--;

if(cur == 0){ if(arr[cur+1]>0){ arr[cur+1]--; can(pos+1); arr[cur+1]++; } }else{ if(arr[cur+1]>0){ arr[cur+1]--; can(pos+1); arr[cur+1]++; }

if(arr[cur-1]>0){
  arr[cur-1]--;
  can(pos+1);
  arr[cur-1]++;
}

} } void solve(){ cin>>n>>m; arr.resize(n+4); inp.resize(m); for(int i=1;i<=n;i++){ arr[i-1]++; arr[i]++; }

for(int i=0;i<m;i++){ cin>>inp[i]; } sort(inp.begin(), inp.end()); can(0);

cout<<"NO"<<endl; } signed main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); solve(); } ~~~~~

I want to know what's causing this, aren't both the same?

Thanks, in advance.

Tags bug in code, #backtracking

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en4 English Rishabh.Jain 2020-10-08 17:02:24 10
en3 English Rishabh.Jain 2020-10-08 16:58:36 9 Tiny change: '[Cards](htt' -> '[Problem: Cards](htt'
en2 English Rishabh.Jain 2020-10-08 16:57:08 176
en1 English Rishabh.Jain 2020-10-08 16:50:17 2312 Initial revision (published)