【map】Winner

描述:

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.

输入

第一行 n表示回合数

接下来n行,

每行按照 名字 分数 的格式输入

输出

只有一行

获胜者的名字

输入样例

样例输入1
3
mike 3
andrew 5
mike 2

样例输入2
3
andrew 3
andrew 2
mike 5

输出样例

样例输出1
andrew

样例输出2
andrew

 怎么形容我看完这一段的心情呢qwq?

题目的大意就是:在伯兰"博客"流行的纸牌游戏的赢家根据以下规则确定:如果在比赛结束时只有一个玩家得分最多,他就是赢家。如果这种玩家的数量超过一个,情况就变得更加复杂了。在每轮比赛中,玩家获得或失去特定数量的积分。在比赛过程中,积分数在"姓名、分数"行中注册,其中名称是玩家的名称,分数是本轮获得的积分数,即整数。如果分数为负数,则表示玩家在回合中失分。因此,如果两个或两个以上的玩家在比赛结束时拥有最高分数(比如,它等于 m),则这些玩家中先赢得m分及以上积分的玩家获胜。最初每个玩家有0分。可以保证,在比赛结束时,至少有一名球员有正数积分。

刚开始我没有理解题目意思,写出了有点bug的代码:

#include<bits/stdc++.h>//除了烧空间什么都不会XwX 
using namespace std;
int main()
{
	std::ios::sync_with_stdio(false);
	map<long long,string> mpl1;
	map<long long,string>::iterator iter;
	map<string,long long> mpl2;
	long long n,i,t;
	string s;
	cin>>n;
	for(i=1;i<=n;i++)
	{
		cin>>s>>t;
		if(mpl2.find(s)==mpl2.end())
		{
			mpl2[s]=t;
			mpl1.insert(pair<long long,string>(t,s)); 
		}
		else
		{
			mpl2[s]+=t;
			mpl1.insert(pair<long long,string>(mpl2[s],s));
		}
	}
	iter=mpl1.end();
	iter--;
	cout<<iter->second;
	return 0;
}

 后来了解了,写出了一个bug更大的程序qwq:

#include<bits/stdc++.h> 
using namespace std;
struct node{
	string date;
	node *next=NULL;
};
int main()
{
	std::ios::sync_with_stdio(false);
	map<long long,node> mpl1;
	map<long long,node>::iterator iter;
	map<string,long long> mpl2;
	node *f,*b;
	long long n,i,t;
	string s;
	cin>>n;
	if(n>0)
	{
		for(i=1;i<=n;i++)
		{
			cin>>s>>t;
			if(mpl2.find(s)!=mpl2.end())
			{
				f=&mpl1[mpl2[s]];
				while(f->next->date!=s)f=f->next;
				f->next=f->next->next;
			}
			mpl2[s]+=t;
			f=&mpl1[mpl2[s]];
			b=new node;
			b->date=s;
			b->next=f->next;
			f->next=b;
		}
		iter=mpl1.end();
		iter--;
		f=&(iter->second);
		while(f->next!=NULL)f=f->next;
		cout<<f->date;
	}
	return 0;
}

 这道题的难点就在于,不可能只输入一遍就把所有信息处理好,所以要在记忆化后排序分数,然后遍历分数最高的人的信息,输出最先达到最高分的人的姓名。

代码如下:

#include<bits/stdc++.h>
using namespace std;
struct node{
	string s;
	long long t;
};
int main()
{
	std::ios::sync_with_stdio(false);
    map<string,long long> m1;
    map<long long,node> m2;
    bool b;
    string s;
    long long n,i,t,maxx;
    cin>>n;
    for(i=1;i<=n;i++)
    {
    	cin>>s>>t;
    	m1[s]+=t;
    	m2[i].s=s;
    	m2[i].t=t;
	}
	map<string,long long>::iterator iter=m1.begin();
	maxx=iter->second;
	for(;iter!=m1.end();iter++)
		if(iter->second>maxx)maxx=iter->second;
	for(iter=m1.begin();iter!=m1.end();iter++)
		if(iter->second<maxx)m1.erase(iter);
	map<long long,node>::iterator ator=m2.begin();
	for(;ator!=m2.end();ator++)
	{
		b=0;
		for(iter=m1.begin();iter!=m1.end();iter++)
			if(iter->first==ator->second.s){b=1;break;}
		if(!b)m2.erase(ator);
	}
	m1.clear();
	for(ator=m2.begin();ator!=m2.end();ator++)
	{
		m1[ator->second.s]+=ator->second.t;
		if(m1[ator->second.s]>=maxx){cout<<ator->second.s;break;}
	}
    return 0;
}

程序实现比较麻烦,虽然有简单很多的方法,但是已经过了,就不想改了qwq。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值