一条以前写过的题居然还WA了好几次。先是没认真读题目,把 at least m 忽略掉了。题解居然是直接模拟三次就好了,反正都是 O(n) 的算法,实际上是一样的。
#include<bits/stdc++.h>
using namespace std;
#define ll long long
int n;
map<string, map<ll, int >>m2;
map<string, ll>m;
int main() {
string name;
ll s;
ll maxs = 0;
string maxname;
while (cin >> n) {
for (int i = 1; i <= n; i++) {
cin >> name >> s;
m[name] += s;
if (m2[name].count(m[name]) == 0)
m2[name][m[name]] = i;
}
/*for (auto i : m2) {
cout << i.first << "\n";
for (auto j : i.second)
cout << " res=" << j.first << " idx=" << j.second << endl;
}*/
ll maxnum = 0;
vector<string>ans;
for (auto i : m) {
if (i.second > maxnum) {
maxnum = i.second;
ans.clear();
ans.push_back(i.first);
}
else if (i.second == maxnum) {
ans.push_back(i.first);
}
}
if (ans.size() == 1)
cout << ans[0] << endl;
else {
int minidx = 1005;
string res;
for (auto name : ans) {
for(auto i:m2[name]){
if (i.first >= maxnum) {
if(i.second<minidx){
minidx = i.second;
res = name;
}
}
}
}
cout << res << endl;
}
}
}