http://codeforces.com/problemset/problem/2/A
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.
所以只有一个只有一个map不行,需要用到两个map,一个找到最大值,另一个在所有达到最大值的人里找谁先达到最大值。
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <string>
#include <map>
using namespace std;
int score[1005];
string name[1005];
int main ()
{
int i,n;
map<string,int> m1,m2;
string s;
cin >> n;
for(i=0;i<n;i++)
{
cin >> name[i] >> score[i];
m1[name[i]] += score[i];
}
map<string,int>::iterator it;
int maxn = -1001;
for(it=m1.begin();it!=m1.end();it++)
{
if(maxn<it->second)
maxn = it->second;
}
for(i=0;i<n;i++)
{
if(m1[name[i]]==maxn)
{
m2[name[i]] += score[i];
if(m2[name[i]]>=maxn)
{
cout << name[i] << endl;
break;
}
}
}
return 0;
}
注意最后一个if语句,m2[name[i]] >= maxn,为什么是大于等于不是等于,就是因为题干中的这一句:who scored at least m points first,一组测试数据:
Input
15
aawtvezfntstrcpgbzjbf 681
zhahpvqiptvksnbjkdvmknb -74
aawtvezfntstrcpgbzjbf 661
jpdwmyke 474
aawtvezfntstrcpgbzjbf -547
aawtvezfntstrcpgbzjbf 600
zhahpvqiptvksnbjkdvmknb -11
jpdwmyke 711
bjmj 652
aawtvezfntstrcpgbzjbf -1000
aawtvezfntstrcpgbzjbf -171
bjmj -302
aawtvezfntstrcpgbzjbf 961
zhahpvqiptvksnbjkdvmknb 848
bjmj -735Answer
aawtvezfntstrcpgbzjbf