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 <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <stack>
#include <map>
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<string, int> 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<string, int>::iterator itr = name_score.begin(); itr != name_score.end(); itr++) {
        max_score = max_score >= itr->second ? max_score : itr->second;
    }

    map<string, bool> max_mans;
    for (map<string, int>::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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值