TLG My code is giving correct answer in all the testcases i am putting but in codechef it is showing wrongs, please help to debug

#include

using namespace std;

int main()
{
int n,*a,*b,diff1=0,diff2=0;

cin>>n;
a=new int[n];
b=new int[n];

int i=0;

for(i=0;i<n;i++)
{
    cin>>a[i]>>b[i];

    if(a[i]>=b[i])
    {
        if(diff1<=a[i]-b[i])
        diff1=a[i]-b[i];
 //       cout<<"diff1 "<<diff1<<endl;
    }else
    {
        if(diff2<=b[i]-a[i])
        diff2=b[i]-a[i];
 //       cout<<"diff2 "<<diff2<<endl;
    }
}

if(diff1>diff2)
cout<<1<<" "<<diff1;
else
cout<<2<<" "<<diff2;

return 0;

}

First of all, I guess you need to re-read the problem statement.
The catch/trick in the problem is that at each round previous scores are added as well, so lead is calculated for the scores and it is very much clearly depicted in the problem statement.

Let me explain you with the test case given :
Round Player 1 Player 2

  1       140          82
  2        89         134 
  3        90         110 
  4       112         106
  5        88          90 
so after round 1 player 1 scores 140 and player 2 scores 82.So lead is 140-82 i.e. 58 and leader is 1. In round 2 player 1 scores 89 and player 2 scores 134. So according to your solution, in this round player 2 is leader and lead equals to 134-89 = 45.

But actually till now the total scores are 229 and 216 for player one and two respectively.
So player 1 is still leading but his lead has been curtailed to just 13. so we need to consider this lead not the individual round difference.

Just include this and you will get accepted. Hope you got it.
PS : If you still have some questions or doubts feel free to ask and reply me @viaan on this thread itself, i’ll try my best to answer you.

2 Likes

@viaan, I have written a new code, after again reading the question …

#include

using namespace std;

int main()
{
int a1,a2,diff1=0,diff2=0,sum1=0,sum2=0,n,i=0;

cin>>n;

for(i=0;i<n;i++)
{
    cin>>a1>>a2;
    sum1+=a1;
    sum2+=a2;

    if(sum1-sum2>0)
    {
        if(diff1<sum1-sum2)
        {
            diff1=sum1-sum2;
        }
    }else
    {
        if(diff2<sum2-sum1)
        {
            diff2=sum2-sum1;
        }
    }
}


if(diff1>diff2)
cout<<1<<diff1;
else
cout<<2<<diff2;


return 0;

}

but here also the answer is coming wrong.

@rajat_121292

have a look at the output section
i guess you are missing the space between the leader and the lead.
cout<<1<<" "<< diff1 ORcout<<"1 "<< diff1
will do good.

Hope I was helpful enough and you learnt something from this problem.

Kudos.

1 Like

Thank you very much…

@rajat_121292
No need to thank me.
It’s my pleasure.
We need to grow as Indian coding fraternity so we all are buddys here.
So If u are satisfied with the answers you can close the thread and chose the question has been answered.

1 Like

viaan can I talk with you personally about some problems of SPOJ, can you give me your email id or fb id., only if you say, I am not forcing…

focus on overall lead score

Your output must consist of a single
line containing two integers W and L,
where W is 1 or 2 and indicates the
winner and L is the maximum lead
attained by the winner.

there L is maximum overall lead, but it needs to be calculated per round