Please help what is wrong with my approach? Codeforces Round #693 (Div. 3) Problem-D

Revision en1, by n_k101, 2022-10-22 21:43:52

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.net/contest/1472/submission/177486281

CORRECT SUBMISSIONhttps://codeforces.net/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;
            }            
        }
Tags contest, *1200, sorting, greedy

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en1 English n_k101 2022-10-22 21:43:52 2121 Initial revision (published)