TLG - Wrong Answer

,

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class test {

    
    public static void main(String[] args) throws Exception {
        int lines;
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String str[];
        
        int p1,p2,win=0,lead=0;
        
        lines = Integer.parseInt(br.readLine());
        
        while(lines-->0)
        {
            str = br.readLine().split(" ");
            
            p1 = Integer.parseInt(str[0]);
            p2 = Integer.parseInt(str[1]);
            
            if(p1>p2)
            {
             
               if(p1-p2>lead)
               {
                   lead=p1-p2;
                   win=1;
               }
               
                
            }
            else
            {
  
                 if(p2-p1>lead)
               {
                  lead=p2-p1;
                   win=2;
               }
            }
            
        }
        
        System.out.println(win+" "+lead);
        
    }
}

why is it giving wrong answer in codechef ??

hi @zargus firstly, why is the class public? it might lead to compilation error.

Coming to the problem with your code, well read the problem statement carefully. You need to add scores of individual players in each round. ie.

Let, s(Pij) denote the score of player i in round j.

then, for any j<=n the score is the sum of all previous rounds plus the current score and then you need to find the lead.

so for player 1 in round 5:
ie s(P15)=s(P11)+s(P12)…+s(P15);

See this image carefully(belongs to the question page itself):

alt text

As you can see

what you have done:
You have found the lead using scoresheet ie the first table itself. But thats not the actual score! that’s just the score for that particular round.

what you need to do:
Refer to the second table and see how actual score comes.
for player 1 the score after round 2 is 140+89, you have considered only 89.
for player 2 the score after round 3 is 82+134+110 you have considered only 110

So, basically just maintain 2 variables say A and B denoting the total score. Instead of comparing (p1-p2) > lead in your code. first do,
A=A+p1 and B=B+p2 and then compare A and B;

And in short, this modification in your code should get you accepted!

Hope this helps! :slight_smile:

3 Likes

thanks dude for explaining it so elaborately got an ac … thnx a lot :slight_smile:

1 Like

@zargus Please accept the answer and vote it up if it has solved your query.

1 Like

and make such replies as comments.

//