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.

题目意思就是给出一个各轮选手得分,最后输出分最高的那个人.

如果有多人分数一样高,那就输出先达到这个分数的人.

反正就是模拟了,直接用stl的map好了.

1.统计每人分数,顺便把每轮的情况也记下来.

2.统计出最高分.

3.遍历每个人的总得分,找到分数最高的那些人.

4.遍历每轮的情况,第一个达到最高分的人,输出就好了.

5.反正就是很水的模拟了.....

#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;

struct pace {
    string name;
    int score;
};
typedef struct pace PACE;
PACE paces[1005];
int pace_num;

int main() {
    pace_num = 0;

    int n;
    scanf("%d", &n);

    char name[35];
    int score;
    map name_score;
    for (int i = 0; i < n; i++) {
        scanf("%s %d", name, &score);
        paces[pace_num].name = name;
        paces[pace_num].score = score;
        pace_num++;

        if (name_score.find(name) == name_score.end()) name_score[name] = 0;
        name_score[name] += score;
    }

    int max_score = -1;
    for (map::iterator itr = name_score.begin(); itr != name_score.end(); itr++) {
        max_score = max_score >= itr->second ? max_score : itr->second;
    }

    map max_mans;
    for (map::iterator itr = name_score.begin(); itr != name_score.end(); itr++) {
        if (itr->second == max_score) max_mans[itr->first] = true;
    }

    int min_pace = 1009;
    string winner;
    name_score.clear();
    for (int i = 0; i < pace_num; i++) {
        if (max_mans.find(paces[i].name) != max_mans.end()) {
            if (name_score.find(paces[i].name) == name_score.end()) name_score[paces[i].name] = 0;
            name_score[paces[i].name] += paces[i].score;
            if (name_score[paces[i].name] >= max_score) {
                winner = paces[i].name;
                goto WINNER_GOT;
            }
        }
    }
WINNER_GOT:
    printf("%s\n", winner.c_str());
    return 0;
}


你可能感兴趣的:(模拟)