题目链接
题目
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.
思路
首先我们先遍历一边求出最大值。
然后我们再模拟一遍看一下哪个人最后为最大值且最早超过最大值,输出即可。
总结
以后遇到类似题时不必执着于一次模拟就求出答案,可以先求出最优解再模拟一次求答案。
Code
// Problem: CF2A Winner
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/CF2A
// Memory Limit: 62 MB
// Time Limit: 1000 ms
//
// Powered by CP Editor (https://cpeditor.org)
#include<bits/stdc++.h>
using namespace std;
//#define int long long
inline int read(){int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;
ch=getchar();}while(ch>='0'&&ch<='9'){x=(x<<1)+
(x<<3)+(ch^48);ch=getchar();}return x*f;}
//#define M
//#define mo
//#define N
int n, m, i, j, k;
map<string, int>mp;
map<string, int>MP;
string s[1010], ans;
int a[1010];
signed main()
{
// freopen("tiaoshi.in", "r", stdin);
// freopen("tiaoshi.out", "w", stdout);
n=read();
for(i=1; i<=n; ++i)
{
cin>>s[i]; a[i]=read();
mp[s[i]]+=a[i];
}
m=-99999999;
for(i=1; i<=n; ++i) m=max(m, mp[s[i]]);
for(i=1; i<=n; ++i)
{
MP[s[i]]+=a[i];
if(mp[s[i]]==m&&MP[s[i]]>=m) return cout<<s[i], 0;
}
return 0;
}