您的位置:首页 > 其它

Codeforces 2A: Winner【STL】【模拟】

2017-07-12 15:44 435 查看
原题链接:http://codeforces.com/contest/2/problem/A

题意描述:

给出n场比赛的结果"name score",name表示姓名,score表示得分,可以为负

在n场比赛完成之后,总分最高的玩家即为赢家,如果出现多个最高分的情况,则看同分的这些玩家中谁的分数首先达到或超过最终的最高分,即为赢家

输出赢家的姓名

思路:

根据题意模拟即可

首先按顺序保存n场比赛的结果,统计总分,如果出现最高分同分的情况就记录下该分数,然后再遍历一遍比赛过程,得出答案

需要注意的是在遍历的过程中如果出现了一名玩家的总分超过了最高分,需要核实该玩家是否是最终最高分同分的那几个玩家,不要傻乎乎的直接认为找到了答案

AC代码

#include <iostream>
#include <cstdio>
#include <vector>
#include <map>
#include <string>
using namespace std;

map<string,int> m;
vector<pair<string,int>> v;
map<string,int> Winners;

int main(){
ios::sync_with_stdio(false);
int n,maxindex=0;
cin>>n;
for(int i=1;i<=n;i++){
string name;
int score;
cin>>name>>score;
v.push_back(pair<string,int>(name,score));
if(m.count(name)==0)
m[name] = score;
else
m[name] += score;
}

int max = 0,cnt = 0;
string ans;
for(auto iter=m.begin();iter!=m.end();iter++)
if(iter->second>max){
max = iter->second;
ans = iter->first;
}
for(auto iter=m.begin();iter!=m.end();iter++)
if(iter->second==max){
cnt++;
Winners[iter->first] = 0;
}

if(cnt>1){
for(auto iter=m.begin();iter!=m.end();iter++)
iter->second=0;
for(int i=0;i<n;i++){
pair<string,int> p = v[i];
m[p.first] += p.second;
if(m[p.first]>=max && Winners.count(p.first)){ans=p.first;break;}
}
}
cout<<ans<<endl;

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: