CF2A Winner(map+思维)

该博客主要讨论了一个名为'Berlogging'的纸牌游戏的获胜规则。游戏结束时,若有多个玩家分数相同且为最高分,则需要找到首位达到这个最高分的玩家作为胜者。博主分享了通过两遍处理数据找出获胜者的巧妙算法思路,强调了处理过程中减分情况的影响。
摘要由CSDN通过智能技术生成

题目描述:
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 mm ) at the end of the game, than wins the one of them who scored at least mm 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.

输入:
The first line contains an integer number nn ( 1<=n<=10001<=n<=1000 ), nn is the number of rounds played. Then follow nn lines, containing the information about the rounds in “name score” format in chronological order, where name is a string of lower-case Latin letters with the length from 1 to 32, and score is an integer number between -1000 and 1000, inclusive.
输出:
Print the name of the winner.

大意:纸牌游戏,进行n回合,每回合一条 名字+分数 信息的组合,找出游戏结束最高分的玩家,如果最高分有多位,找出第一个达到最高分的玩家

思路:
先把结束时的最高分找出来,然后按照输入顺序去找第一个到达最大分的即可,那么如何找最高分玩家里第一个达到最大分的呢,首先这个玩家的最终分要等于最高分,其次在过程中这个玩家的分数要第一个 >= 最大分,有大于是因为他可能掉分,但是他第一个达到了

#include<bits/stdc++.h>
using namespace std;
 
typedef unsigned long long ull;
typedef long long ll;
 
const ll maxx = 1e18;
const int N = 1e6+100;
const int ps = 1e4+10;
const double eps = 1e-8;

map<string,int >mp,mps;
int n,k,max1;
string s[1001],ans;
int a[1001];

int main()
{
	cin>>n;
	
	for(int i=1;i<=n;i++)
	{
		cin>>s[i]>>a[i];
		mp[s[i]]+=a[i];
	}//第一遍处理,记录最终状态
	
	for(auto k : mp)
	{
		max1=max(max1,k.second);
	}//找出最大分
	
	for(int i=1;i<=n;i++)
	{
		mps[s[i]]+=a[i];
		if(mp[s[i]]==max1&&mps[s[i]]>=max1)
		{
			ans=s[i];
			break;
		}
	}//第二遍处理,找出分数等于最大分且在过程中第一个 >= 最大分的选手
	
	cout<<ans;

}

反思:
这个题的思路特别巧妙,相当于把处理过程做了两遍,第一遍先找出最高分,第二遍找出最先达到最高分的选手,注意可能会有减分的影响;

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

草莓猫猫软糖

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值