The link to problem : TLG
My code :
/* PROB - TLG */
#include <stdio.h>
int main(void) {
// your code goes here
int t,win=1,diff=-1,lead=0,s1,s2;
scanf("%d\n", &t);
while(t--)
{
scanf("%d\n", &s1);
scanf("%d\n", &s2);
if(diff==-1)
{
if(s1>s2)
{
lead = diff = s1-s2;
win = 1;
}
else
{
lead = diff = s2-s1;
win = 2;
}
}
else
{
if(s1>s2)
{
diff = s1-s2;
if(diff>lead)
{
lead=diff;
win = 1;
}
}
else
{
diff = s2-s1;
if(diff>lead)
{
win = 2;
lead = diff;
}
}
}
}
printf("%d %d\n", win, lead);
return 0;
}
The scores of previous round accumulate!!
Meaning, Take this example-
2
140 82
89 155
In first case, player 1 leads by 58 points. In second round, ALTHOUGH player 2 achieves a lead of 66, we have to see the accumulated scores. So lead for round 2 is (82+155)-(140+89)=8 .
I got AC when i followed this logic. Refer to the table given in problem statement for more details.
1 Like
Your code finds the difference for each round individually but the question requires you to find the difference between the cumulative scores till that round.
2 Likes
Even i got stuck on that part while solving it XD. A good lesson on reading problem statement minutely haha.
1 Like
Got that. I think i might’ve just skimmed past that table.
2 Likes
@vijju123 u may close this post as well
Solution not getting accepted, what’s wrong here?
#include
#include <math.h>
using namespace std;
int CalculateAbs(int val1, int val2){
return abs(val2-val1);
}
int AddValue(int to, int from){
int value = to + from;
return value;
}
int InputValue(){
int a;
cin >> a;
return a;
}
void OutputValue(int player, int diff){
cout << player << " " << diff;
}
int main() {
int round, playerA=0, playerB=0, max=0, currentA, currentB, turn;
round = InputValue();
while(round–){
currentA = InputValue();
currentB = InputValue();
playerB = AddValue(playerB, currentB);
playerA = AddValue(playerA, currentA);
if(CalculateAbs(playerA, playerB) >= max){
max = CalculateAbs(playerA, playerB);
if(playerB > playerA)
turn = 2;
turn = 1;
}
}
OutputValue(turn, max);
return 0;
}