【Codeforces Beta Round 2A】【模拟 map 有map的java实现哦】Winner 第一个达到最终最高分的人名

A. Winner
time limit per test
1 second
memory limit per test
64 megabytes
input
standard input
output
standard output

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.

Input

The first line contains an integer number n (1  ≤  n  ≤  1000), n is the number of rounds played. Then follow n 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.

Output

Print the name of the winner.

Sample test(s)
input
3
mike 3
andrew 5
mike 2
output
andrew
input
3
andrew 3
andrew 2
mike 5
output
andrew


#include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<math.h>
#include<iostream>
#include<string>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<bitset>
#include<algorithm>
#include<time.h>
using namespace std;
void fre(){freopen("c://test//input.in","r",stdin);freopen("c://test//output.out","w",stdout);}
#define MS(x,y) memset(x,y,sizeof(x))
#define MC(x,y) memcpy(x,y,sizeof(x))
#define MP(x,y) make_pair(x,y)
#define ls o<<1
#define rs o<<1|1
typedef long long LL;
typedef unsigned long long UL;
typedef unsigned int UI;
template <class T> inline void gmax(T &a,T b){if(b>a)a=b;}
template <class T> inline void gmin(T &a,T b){if(b<a)a=b;}
const int N=1000+5,M=0,Z=1e9+7,ms63=1061109567;
map<string,int>mop;
char s[N][36];
int v[N];
int n;
int main()
{
	while(~scanf("%d",&n))
	{
		mop.clear();
		for(int i=1;i<=n;++i)
		{
			scanf("%s%d",s[i],&v[i]);
			mop[s[i]]+=v[i];
		}
		int maxv=-1e9;
		for(map<string,int>::iterator it=mop.begin();it!=mop.end();++it)gmax(maxv,it->second);
		for(map<string,int>::iterator it=mop.begin();it!=mop.end();++it)
		{
			if(it->second==maxv)it->second=0;
			else it->second=-1e9;
		}
		for(int i=1;i<=n;++i)
		{
			mop[s[i]]+=v[i];
			if(mop[s[i]]>=maxv)
			{
				puts(s[i]);
				break;
			}
		}
	}
	return 0;
}
/*
【trick&&吐槽】
模拟题,脑补题意就是作大死呀!
1,比赛的人数不只是有2个,而是有若干个
2,最高分应在最后统计,因为过程中达到的最高分,不代表最后全局还拥有这个最高分(可能被减掉)

【题意】
若干个人参加比赛。
有n(1<=n<=1000)次得分情况。
每次得分情况,我们会告诉你(人名 得分),得分范围是[-1000,1000]
1,获胜者要求最后得分最高
2,出现重复则输出第一个达到最高分(是大于等于不是等于)这个分数的人名。

【类型】
模拟

【分析】
直接用map模拟即可。可以先模拟一遍,统计一个最高分。
然后再模拟一遍,凡是最后是最高分的人,第一个达到最高分者就为胜者。

【时间复杂度&&优化】
O(n * lg(map常数) )

*/

import java.util.*;
import java.math.*;
public class Main
{
	static Scanner cin = new Scanner(System.in);
	public static void main(String[] args)
	{
		HashMap<String,Integer> mop=new HashMap<String,Integer>();
		String a[]=new String[1010];
		int b[]=new int[1010];
		int n=cin.nextInt();
		for(int i=1;i<=n;++i)
		{
			a[i]=cin.next();
			b[i]=cin.nextInt();
			int x=b[i];
			if(mop.containsKey(a[i]))x+=(int)mop.get(a[i]);
			mop.put(a[i],x);
		}
		int maxv=Integer.MIN_VALUE;
		for(Object obj:mop.keySet())maxv=Math.max(maxv,(int)mop.get(obj));
		for(Object obj:mop.keySet())
		{
			if((int)mop.get(obj)==maxv)mop.put(obj.toString(),0);
			else mop.put(obj.toString(),Integer.MIN_VALUE);
		}
		for(int i=1;i<=n;i++)
		{
			int x=b[i];
			if(mop.containsKey(a[i]))x+=(int)mop.get(a[i]);
			mop.put(a[i],x);
			if(x>=maxv)
			{
				System.out.println(a[i]);
				break;
			}
		}
	}
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值