codeforces 2A Winner

A. Winner
time limit per test
1 second
memory limit per test
64 megabytes
input
standard input
output
standard output

The winner of the card game popular in Berland "Berlogging" is determined according to the following rules. If at the end of the game there is only one player with the maximum number of points, he is the winner. The situation becomes more difficult if the number of such players is more than one. During each round a player gains or loses a particular number of points. In the course of the game the number of points is registered in the line "name score", where name is a player's name, and score is the number of points gained in this round, which is an integer number. If score is negative, this means that the player has lost in the round. So, if two or more players have the maximum number of points (say, it equals to m) at the end of the game, than wins the one of them who scored at least m points first. Initially each player has 0 points. It's guaranteed that at the end of the game at least one player has a positive number of points.

Input

The first line contains an integer number n (1  ≤  n  ≤  1000), n is the number of rounds played. Then follow n lines, containing the information about the rounds in "name score" format in chronological order, where name is a string of lower-case Latin letters with the length from 1 to 32, and score is an integer number between -1000 and 1000, inclusive.

Output

Print the name of the winner.

Examples
input
Copy
3
mike 3
andrew 5
mike 2
output
andrew
input
Copy
3
andrew 3
andrew 2
mike 5
output

andrew


题目的大意:几个人在玩一个游戏,初始的分数都是0,每回合由某人给出一个分数(可以为负),负的话总分就减去这个数,求游戏结束时分数最高的人的名字,如果有几个人的总分相同的话,就输出最先到达这个分数的男人(可以高于这个最终总分)。


#include <bits/stdc++.h>
int maxn=-1e7;
using namespace std;
struct Gamer
{
	string name;
	int score;
}a[1002];
int main()
{
	int n;
	cin>>n;
	map<string,int>b;
	map<string,int>c;
	for(int p=0; p<n; p++)
	{
		cin>>a[p].name>>a[p].score;
		b[a[p].name]+=a[p].score;
	}
    for(auto i: b)
    {
    	if(maxn<i.second)
    	maxn=i.second;
	}
	string ans;
	int j;
	for(j=0; j<n; j++)      
	{
		c[a[j].name]+=a[j].score;
		if(c[a[j].name]>=maxn && b[a[j].name]==maxn) //这里傻了,开始时写了两个等来判断,其实是可以高于最终总分的 
		{
			ans=a[j].name;
			break;
		}
	}
	cout<<ans<<endl;
	return 0;
} 
/*
给一组数据:
15
aawtvezfntstrcpgbzjbf 681          681
zhahpvqiptvksnbjkdvmknb -74        -74
aawtvezfntstrcpgbzjbf 661          1342
jpdwmyke 474                       474
aawtvezfntstrcpgbzjbf -547         795  
aawtvezfntstrcpgbzjbf 600          1395
zhahpvqiptvksnbjkdvmknb -11        -85
jpdwmyke 711                       1185
bjmj 652                           652
aawtvezfntstrcpgbzjbf -1000        395
aawtvezfntstrcpgbzjbf -171         224
bjmj -302                          350
aawtvezfntstrcpgbzjbf 961          1185
zhahpvqiptvksnbjkdvmknb 848        763
bjmj -735                          -1085
 
*/ 

猜你喜欢

转载自blog.csdn.net/rem_little_fan_boy/article/details/79660695
今日推荐