题目意思就是给出一个各轮选手得分,最后输出分最高的那个人.
如果有多人分数一样高,那就输出先达到这个分数的人.
思路大概是,把数据都录进去然后按分数排序,如果分数相同按最后一次出现该名词时的下标排序
样例可以过,感觉思路是对的,但是总是第六次测验wa,请大佬指正;
#include
#include
#include
#include
#include
using namespace std;
struct node{
int score = 0;
int index;
};
bool cmp(const pair& a, const pair& b)
{
if((a.second).score != (b.second).score)
return (a.second).score > (b.second).score;
else
return (a.second).index < (b.second).index;
}//按规则排序
int main()
{
ios::sync_with_stdio(false);
int T; //测试用例个数
cin >> T;
mapnums;
for(int i = 0;i < T;i++)//数据录入
{
string a;
int b;
cin >> a >> b;
nums[a].score += b;//记分
nums[a].index = i;//有相同分数时用于比较得到该分数的顺序先后。
}
vector< pair > x(nums.begin(),nums.end());//把map放到容器中排序
sort(x.begin(),x.end(),cmp);
cout << x[0].first <<endl;
return 0;
}