Codeforces 2A Winner (map运用)

Winner

Time Limit: 1000ms

Memory Limit: 65536KB

This problem will be judged on CodeForces.
Original ID: 2A

64-bit integer IO format: %I64d     
Java class name: (Any)

Prev

Submit Status Statistics Discuss

Next

Type:

None

None Graph Theory 
    2-SAT     Articulation/Bridge/Biconnected Component
     Cycles/Topological Sorting/Strongly Connected Component
     Shortest Path 
        Bellman Ford         Dijkstra/Floyd Warshall
     Euler Trail/Circuit 
    Heavy-Light Decomposition     Minimum Spanning Tree
     Stable Marriage Problem 
    Trees     Directed Minimum Spanning Tree
     Flow/Matching 
        Graph Matching             Bipartite Matching
             Hopcroft–Karp Bipartite Matching
             Weighted Bipartite Matching/Hungarian Algorithm
         Flow 
            Max Flow/Min Cut             Min Cost Max Flow
 DFS-like 
    Backtracking with Pruning/Branch and Bound 
    Basic Recursion     IDA* Search 
    Parsing/Grammar     Breadth First Search/Depth First Search
     Advanced Search Techniques 
        Binary Search/Bisection         Ternary Search
 Geometry 
    Basic Geometry     Computational Geometry
     Convex Hull 
    Pick‘s Theorem Game Theory 
    Green Hackenbush/Colon Principle/Fusion Principle 
    Nim     Sprague-Grundy Number 
Matrix     Gaussian Elimination 
    Matrix Exponentiation Data Structures 
    Basic Data Structures     Binary Indexed Tree
     Binary Search Tree 
    Hashing     Orthogonal Range Search 
    Range Minimum Query/Lowest Common Ancestor 
    Segment Tree/Interval Tree     Trie Tree
     Sorting 
    Disjoint Set String 
    Aho Corasick     Knuth-Morris-Pratt 
    Suffix Array/Suffix Tree Math 
    Basic Math     Big Integer Arithmetic 
    Number Theory         Chinese Remainder Theorem
         Extended Euclid 
        Inclusion/Exclusion         Modular Arithmetic
     Combinatorics 
        Group Theory/Burnside‘s lemma         Counting
     Probability/Expected Value 
Others     Tricky 
    Hardest     Unusual 
    Brute Force     Implementation 
    Constructive Algorithms     Two Pointer 
    Bitmask     Beginner 
    Discrete Logarithm/Shank‘s Baby-step Giant-step Algorithm 
    Greedy     Divide and Conquer 
Dynamic Programming                  
Tag it!

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 Input

Input

3mike 3andrew 5mike 2

Output

andrew

Input

3andrew 3andrew 2mike 5

Output

andrew

Source

Codeforces Beta Round #2

题意:大概就是说一个报数的游戏,没人报一个数字,游戏结束后数字之和最大的获胜,如果相同,则先报的胜利.

此题可用数组模拟,但是有点麻烦,用STL里的map可以很快解决此问题.

AC代码:

#include <iostream>
#include <cstring>
#include <map>
using namespace std;
map<string,int>m1,m2;
string name[1010];
int score[1010];
int main()
{
    int n;
    cin>>n;
    for(int i=0; i<n; i++)
    {
        cin>>name[i]>>score[i];
        m1[name[i]]+=score[i];
    }
    int maxx=-1005;
    for(int i=0;i<n;i++)
    {
        if(m1[name[i]]>maxx)
            maxx=m1[name[i]];
    }
    for(int i=0;i<n;i++)
    {
        m2[name[i]]+=score[i];
        if(m2[name[i]]>=maxx&&m1[name[i]]>=maxx)
        {
            cout<<name[i]<<endl;
            break;
        }
    }
    return 0;
}

网上看到了q神的代码,菜鸟要学习一下

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<map>
using namespace std;
const int MAXN=1005;
string s[MAXN];
int c[MAXN];
map<string,int>mp;
int main()
{
    ios::sync_with_stdio(false);
    int n;
    cin>>n;
    for(int i=0; i<n; i++)
        cin>>s[i]>>c[i];
    for(int i=0; i<n; i++)
        mp[s[i]]+=c[i];
    int res=0;
    for(map<string,int>::iterator itr=mp.begin(); itr!=mp.end(); itr++)
        res=max(res,itr->second);
    for(map<string,int>::iterator itr=mp.begin(); itr!=mp.end();)
    {
        if(itr->second !=res)
            mp.erase(itr++);//函数删除在pos位置的元素
        else
            (itr++)->second =0;
    }
    for(int i=0; i<n; i++)
        if(mp.find(s[i])!=mp.end())
        {
            mp[s[i]]+=c[i];
            if(mp[s[i]]>=res)
            {
                cout<<s[i]<<endl;
                return 0;
            }
        }
    return 0;
}
时间: 2024-11-09 04:25:04

Codeforces 2A Winner (map运用)的相关文章

CodeForces 2A Winner

Winner Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice CodeForces 2A Description The winner of the card game popular in Berland "Berlogging" is determined according to the following rules.

CodeForces 2A - Winner(模拟)

题目链接:http://codeforces.com/problemset/problem/2/A 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 acc

CF 2A Winner(STL+模拟)

题目链接:http://codeforces.com/contest/2/problem/A 题目: 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,

CodeForces 34D Road Map

给出每个点的父节点,存下来. 再从r2开始,dfs到r1,把这条路径上的所有结点的父节点改变方向就可以了. #include <iostream> #include <cstdlib> #include <cstring> #include <string> #include <cstdio> #include <cmath> #include <algorithm> #include <vector> #in

Codeforces.566E.Restoring Map(构造)

题目链接 \(Description\) 对于一棵树,定义某个点的邻居集合为所有距离它不超过\(2\)的点的集合(包括它自己). 给定\(n\)及\(n\)个点的邻居集合,要求构造一棵\(n\)个点的树,使得每个给定的集合都对应一个点.输入保证有解. \(n\leq1000\). \(Solution\) 如果两个点的邻居集合大小为\(2\),那么交集中的两个点之间一定有边.这样我们就可以\(O(\frac{n^3}{w})\)确定出非叶节点以及它们之间的连边. 然后考虑叶节点应该挂到哪里.如果

Codeforces Beta Round 2 A题:Winner

题目传送门 这道题我开始准备使用map+stable_sort来做,但是无论是map或是unorder_map,都会改变输入的顺序,导致某一组答案错误. 所以我们可以使用map来进行纯模拟的操作. 首先,在输入的同时把最终各个人的分数加起来,然后找到最大值. 其次,再次模拟一遍游戏,当找到第一个达到m且最终的分数也大于等于m的人,输出即可. 12345678910111213141516171819202122232425262728293031323334 #define rep(i,a,n)

Codeforces 755B:PolandBall and Game(map+思维)

http://codeforces.com/problemset/problem/755/B 题意:A可以喊出n个字符串,B可以喊出m个字符串,如果一个字符串之前被喊过,那么它再也不能喊了,A先喊,最后没得喊的输.问谁赢. 思路:用map判断A.B中字符串一样的个数,因为A可以先喊,然后B再喊,每个人可以喊的数量要减去cnt / 2(如果是奇数,A可以喊多一个),表示被对方喊了.如果最后n<=m则A输,否则A赢,等于的情况是因为A先喊,B再喊,到最后A会输. 1 #include <cstdi

CodeForces 567C. Geometric Progression(map 数学啊)

题目链接:http://codeforces.com/problemset/problem/567/C C. Geometric Progression time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Polycarp loves geometric progressions very much. Since he was on

map Codeforces Round #Pi (Div. 2) C. Geometric Progression

题目传送门 1 /* 2 题意:问选出3个数成等比数列有多少种选法 3 map:c1记录是第二个数或第三个数的选法,c2表示所有数字出现的次数.别人的代码很短,思维巧妙 4 */ 5 /************************************************ 6 * Author :Running_Time 7 * Created Time :2015-8-6 1:07:18 8 * File Name :C.cpp 9 *************************