n_k101's blog

By n_k101, history, 2 years ago, In English

2 pointer with greedy

I tried to solve the problem using 2 pointer with greedy. It was failing at 1146th test case

My approach was to check if adding the element x to current score will increase it compared to opponents score than add it otherwise just not let the opponent to take their score.

My logic was similar to one of the logics that I found in submissions. The new approach was that I didn.t included the current score of the player and compared only the values if it is greater than that of opponents then add it to the score. This solution worked out. But according to me even if I compare using the current score the answer should be more accurate, which is not the case, what can be wrong please help.

WRONG SUBMISSIONhttps://codeforces.me/contest/1472/submission/177486281

CORRECT SUBMISSIONhttps://codeforces.me/contest/1472/submission/177486439

CODE TO BE COMPARED IN main()

WRONG

 while(i<ev || j<od){
            if(f){
                if(i<ev &&(j==od || a+e[i]>=b+o[j])){
                    a+=e[i];i++;
                }else{                    
                    j++;
                }
                f=0;
            }else{
                if(j<od && (i==ev || b+o[j]>=a+e[i])){
                    b+=o[j];j++;
                }else{                    
                    i++;
                }
                f=1;
            }            
        }

CORRECT

 while(i<ev || j<od){
            if(f){
                if(i<ev &&(j==od || e[i]>=o[j])){
                    a+=e[i];i++;
                }else{                    
                    j++;
                }
                f=0;
            }else{
                if(j<od && (i==ev || o[j]>=e[i])){
                    b+=o[j];j++;
                }else{                    
                    i++;
                }
                f=1;
            }            
        }
  • Vote: I like it
  • 0
  • Vote: I do not like it

| Write comment?