WA in Lead Game (LEADGAME)

Following is the solution which I coded for the Lead Game (CodeChef: Practical coding for everyone):-

#include <bits/stdc++.h>
using namespace std;

int main()
{
    int n;
    cin >> n;
    
    int w; 
    int l = 0;
    int s, t;
    
    while(n--)
    {
        cin >> s >> t;
        if(abs(s-t) > l)
        {
            if(s > t)
            {
                w = 1;
                l = s-t;
            }
            else
            {
                w = 2;
                l = t-s;
            }
        }
    }
    
    cout << w << " " << l << "\n";
    
    return 0;
}

It works perfectly fine for the sample test case and for some other test cases but gives WA on submitting. Going through the AC submissions for the problem I found the following code:-

#include <iostream>
using namespace std;
int main()
{
	int t,n,m,s=0,max1=0,max2=0,k,sum1=0,sum2=0;
	cin >> t; // Enter Test Case
	while(t--)
	{
		cin >> n >> m; // Enter Player1 score and Player2 score
		sum1 = sum1 + n; // Goes on adding score of Player1
		sum2 = sum2 + m; // Goes on adding score of Player2
		if(sum1>sum2) 
			max1=sum1-sum2; // Player1 score is greater
		if(sum1<sum2)
			max2=sum2-sum1; // Player2 score is greater
		if(max1>s) 
		{
			s=max1;  // If player1 score difference is greater set s=max1
			k=1;
 
		}
		if(max2>s)
		{
			s=max2; //If player2 score difference is greater set s=max1
			k=2;
		}
 
	}
	cout << k <<" "<< s << endl;
}

For the following input:-

8
140 82
89 134
90 110
112 106
88 90
80 160
24 68
100 40

The above code gives the following output:-

2 127

Whereas my code gives the following output:-

2 80

And from the input one can clearly see that Player 2 is the winner with a lead of 80, then where is this 127 coming from? And then why am I getting WA when the AC solution gives an incorrect output? Please help.

Note:- I am not at all trying to insult the user who posted the above solution. I simply have a doubt.

@everule1 Please help.

2 Likes

Thank you so much !!! The last line of the third paragraph of the problem statement is very deceptive.

#include
#include <bits/stdc++.h>
#include
#include <stdlib.h>
#include
using namespace std;
main()
{
long int t,a,b,win,lead=0;
cin>>t;
for(int i=0;i<t;i++)
{
cin>>a>>b;
if(a>b)
{
if(a-b>lead)
{
lead=a-b;
win=1;
}
}
else if(b>a)
{
if(b-a>lead)
{
lead=b-a;
win=2;
}
}
}
cout<<win<<" "<<lead<<endl;
}

this works fine in my ide for all test cases but i got WA

https://www.codechef.com/viewsolution/30513225
See this, You will get what’s wrong with your code