26 November 2014

Quiz 52: Find winner of card game

Problem:
Few friends are playing cards. There play N rounds. Winner of each round is in format "name points". Points of all other players for that round is 0. Final Winner is the person with maximum total points of all rounds. It is guaranteed that no 2 players will have same maximum total after N rounds. Find the name of person and his/her total score

Input Format: 
N(number of rounds)
N lines in format "name points"

Output Format: 
Name Total_points

Constraints: 
None

Sample Input
10
priya 10
satyam 7
shikha 6
sid 11
amit 7
shikha 6
amit 1
amit 1
amit 2
satyam 4

Sample Output:
shikha 12

Explanations:
Shikha have 6+6=12 points, which are maximum.


Solution:

chomp($t=<STDIN>);
for($i=0;$i<$t;$i++)
{
chomp($line=<STDIN>);
push(@arr,$line);
}
foreach(@arr){
($a,$b)=split/ /;
$n{$a}+=$b;
}
$max= (sort {$b<=>$a} values %n)[0];
for(@arr){
($a,$b)=split/ /;
$m{$a}+=$b;
if ($max == $n{$a}) {$ans=$a; last }
}
print "$ans $max";


Tips:
use of PERL hashes

No comments:

Post a Comment