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;
}
题意描述:
给出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;
}
相关文章推荐
- codeforces 2A winner(stl应用,模拟)
- CodeForces 2A - Winner(模拟)
- CodeForces 2A - Winner(模拟)
- Codeforces 2A. Winner(字符串:模拟)
- CodeForces 2A - Winner(模拟)
- Codeforces 2A-Winner(模拟)
- CodeForces 2A Winner(CodeForces的题意总是那么难理解)——Codeforces Beta Round #2
- codeforces 2A Winner
- CodeForces 2A Winner(map模拟)
- 【Codeforces Beta Round 2A】【模拟 map 有map的java实现哦】Winner 第一个达到最终最高分的人名
- codeforces 2A Winner
- codeforces 2A. Winner
- CodeForces - 2A - Winner
- CodeForces 2A Winner
- codeforces 2A. Winner
- codeforces 2A Winner
- Codeforces--367D--D. Sereja ans Anagrams(STL)(模拟)
- Codeforces 2A :winner
- Codeforces 2A :winner
- 【STL】【模拟】Codeforces 696A Lorenzo Von Matterhorn