CF2A Winner(map+思维)

简介: CF2A Winner(map+思维)

题目描述:

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;
}


反思:

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

目录
相关文章
|
JavaScript 前端开发
jQuery中绑定事件的方式有几种
jQuery中绑定事件的方式有几种
97 1
|
存储 Java 索引
笨办法学 Java(四)(2)
笨办法学 Java(四)(2)
72 0
每日一题 --- 2016. 增量元素之间的最大差值[力扣][Go]
每日一题 --- 2016. 增量元素之间的最大差值[力扣][Go]
每日一题 --- 2016. 增量元素之间的最大差值[力扣][Go]
|
存储 IDE Java
NDK 系列(6):说一下注册 JNI 函数的方式和时机
NDK 系列(6):说一下注册 JNI 函数的方式和时机
210 0
NDK 系列(6):说一下注册 JNI 函数的方式和时机
leetcode 144 145 94二叉树的三种递归遍历
leetcode 144 145 94二叉树的三种递归遍历
93 0
【Groovy】MOP 元对象协议与元编程 ( 使用 Groovy 元编程进行函数拦截 | 通过 MetaClass#invokeMethod 方法调用类其它方法 )
【Groovy】MOP 元对象协议与元编程 ( 使用 Groovy 元编程进行函数拦截 | 通过 MetaClass#invokeMethod 方法调用类其它方法 )
224 0
【Groovy】MOP 元对象协议与元编程 ( 使用 Groovy 元编程进行函数拦截 | 通过 MetaClass#invokeMethod 方法调用类其它方法 )
|
存储
享元模式与组合模式(2)
享元模式与组合模式(2)
255 0
享元模式与组合模式(2)
|
11天前
|
弹性计算 关系型数据库 微服务
基于 Docker 与 Kubernetes(K3s)的微服务:阿里云生产环境扩容实践
在微服务架构中,如何实现“稳定扩容”与“成本可控”是企业面临的核心挑战。本文结合 Python FastAPI 微服务实战,详解如何基于阿里云基础设施,利用 Docker 封装服务、K3s 实现容器编排,构建生产级微服务架构。内容涵盖容器构建、集群部署、自动扩缩容、可观测性等关键环节,适配阿里云资源特性与服务生态,助力企业打造低成本、高可靠、易扩展的微服务解决方案。
1244 5
|
10天前
|
机器学习/深度学习 人工智能 前端开发
通义DeepResearch全面开源!同步分享可落地的高阶Agent构建方法论
通义研究团队开源发布通义 DeepResearch —— 首个在性能上可与 OpenAI DeepResearch 相媲美、并在多项权威基准测试中取得领先表现的全开源 Web Agent。
1225 87