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
3 mike 3 andrew 5 mike 2
output
andrew
input
3 andrew 3 andrew 2 mike 5
output
andrew
划线部分均为曾经理解错误的地方 这道题目的意思是指,有一群人在玩游戏,每次都可以得到一个分数,这个分数可以是正数,也可以是负数,如果最后过程中 ,有人的累计分数小于零,则意味着出局, 后面得的分数再高也无意义求出分数最大的人的名字,如果最后得到的最大分数有很多人,则按照第一个得到这个分数的人胜利。
这道题目明显是用容器map来写,虽然map可以进行排序,但是map是按照键值来进行排序的,而且,有可能有多个相同的数据,所以,不能够仅仅写一次,可以写三遍for循环进行鉴别,第一层,将每个人所得到的分数进行累加,得到所有人的最后分数, 第二层,逐一判断,确认最大的分数是多少, 第三层,再进行一次新的累加,确认最先达到最大分数的人,输出结果。
代码
#include<map> #include<string> #include<iostream> #include<algorithm> using namespace std; string a[10100]; int b[10100]; int main() { int n; map<string,int> c,d; cin>>n; for(int i=0;i<n;i++) { cin>>a[i]>>b[i]; c[a[i]]+=b[i]; } int maxx=0; for(int i=0;i<n;i++) { if(c[a[i]]>maxx) maxx=c[a[i]]; } for(int i=0;i<n;i++) { d[a[i]]+=b[i]; if(d[a[i]]>=maxx&&c[a[i]]==maxx) { cout<<a[i]<<endl; break; } } return 0; }