I’m trying to brush up my algorithmic skills and came across this problem. Here goes: http://opc.iarcs.org.in/index.php/problems/LEADGAME
My code is:
#include <iostream>
#include <cmath>
using namespace std;
int main(int argc, char* argv[])
{
int count;
cin >> count;
int winningP = 1; // winning player
int lead = 0; // lead
for (int i=0; i < count; i++)
{
int scoreA, scoreB = 0;
cin >> scoreA >> scoreB;
int l;
if (scoreA > scoreB)
l = scoreA - scoreB;
else
l = scoreB - scoreA;
if (l > lead) // greater lead than what's been processed
{
lead = l;
winningP = scoreA > scoreB ? 1 : 2;
}
}
cout << winningP << " " << lead;
return 0;
}
However, on the website, when I submit my code for evaluation, it prints out that my program is giving wrong answers. What am I doing wrong here? The sample input and outputs have been verified.
You are solving a different problem. You find the player who won any round by the biggest margin (and that margin). In the problem, the scores of the rounds are accumulated, so if, as in the example, player 1 wins the first round by 58 and loses the second by 45, after two rounds, player1 still leads by 13.