Need help in correcting flappy Bird solution of mine

In the question june codechef starters Flappy Bird (link-https://www.codechef.com/START5B/problems/FLAPPYBD)

ihave written a solution but unfortunataly it is wrong i am not able to get why.
If anyone can help it would be of great help.Any help will be appreciated.
Link to sol-https://www.codechef.com/viewsolution/48300736
(Code is provided below too)
What i did is first checked if anywhere this game cannot be completed (used variable value for it).
If not then i moved step by step from starting position to X1, then from X1 to X2 and so on.
I took the minimum clicks tp reach Xi but if anytime it cant move forward i adjusted the number of clicks(clicks which i havent used earlier where i could have). Variable c refers to clicks needed to go from Xi to Xi+1, click is used to fnd total no of clicks.

Thank you.

using namespace std;

int gcd(int a,int b){
    if(a%b==0)
        return b;
    else
        return gcd(b,a%b);
}

void solve(){
    int t;
    cin>>t;
    while(t--){
        int n,H;
        cin>>n>>H;
        int x[n];
        int h[n];
        int value=0;
        for(int i=0;i<n;i++)
            cin>>x[i];
        for(int i=0;i<n;i++) {
            cin >> h[i];
            if(h[i]>=x[i]+H)
                value=-1;
        }

        int height=H;
        int clicks=0;
        int pos=0;

        for(int i=0;i<n;i++) {
          int c=ceil(double(h[i]+1+x[i]-pos-height)/2);

          if(c>0){
              if(c>x[i]-pos){
                if(pos-clicks>c-x[i]+pos) {
                    clicks=clicks+c;
                    pos = x[i];
                    height = h[i] + 1;
                }
                else{
                    value=-1;
                    pos = x[i];
                    height = h[i] + 1;
                }
              }
              else{
                  clicks=clicks+c;
                  pos=x[i];
                  height=h[i]+1;
              }
          }
          else {
              int dist=x[i]-pos;
              if(height-dist>=h[i]+1){
                pos=x[i];
                height=height-dist;
              }
          }

        }
        if(value==-1) {
            cout<<-1<<"\n";
            goto label;
        }
        cout<<clicks<<"\n";

        label:;
    }
}
int main(){

#ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    freopen("error.txt","w",stderr);
#endif
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    solve();
    return 0;
}

Your approach is a bit wrong buddy
see this test case:-
1
5 10
1 3 4 6 9
8 12 13 15 18

Your O/P :- -1
Correct O/P :- 9 , he will increase his height every second, and he will get to heights
11,13,14,16,19 at the end of every x[ i ]

1 Like

Thanks for the help.

1 Like