Codeforces 2A. Winner

本文分享了一道ACM模拟题的解题思路及代码实现,通过三次遍历找到最高分数及对应最早出现的名字,使用C++ map记录数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一条以前写过的题居然还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;
		}
	}

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值