题解:B4195 [2023 海淀区小学组] 纸牌游戏

· · 题解

题目简述

每个人有自己的名字和分数,分数会在游戏过程中加或减,最后输出分数最高者的名字,有多个最高则输出先获得这个分数的人的名字。

主要思路

由于人的分数在游戏过程中会减少,所以不能在输入过程中就计算最高分数,这样在最高分数减分后,不会改变目前的最高分数。

可以创建一个结构体 node

输入时先用 unordered_map 存下来这些信息,然后遍历 unordered_map,依次将这些人的信息存入 node 数组,最后数组按照题意结构体排序,输出排序后第 1 个人的名字。

AC Code

#include<map>
#include<set>
#include<queue>
#include<cmath>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<unordered_map>
#include<unordered_set>
using namespace std;

namespace IO {
    #ifdef ONLINE_JUDGE
    #define getchar getchar_unlocked
    #endif
    #define pc putchar
    #define gc getchar
    template<typename T> void read(T &x) { int f = 1; x = 0; char ch = gc(); while (!isdigit(ch)) { if (ch == '-')f = -1; ch = gc(); }while (isdigit(ch)) { x = (x << 1) + (x << 3) + (ch ^ 48); ch = gc(); }x *= f; }
    template<typename T, typename ...Args> void read(T &x, Args &...args) { read(x); read(args...); }
    template<typename T> void print(T x) { if (x < 0) { pc('-'); x = -x; }if (x > 9) { print(x / 10); }pc(char(x % 10 + 48)); }
    template<typename T, typename ...Args> void print(T &x, Args &...args) { print(x); pc(' '); print(args...); }
    inline void readstr(string& x) { x.clear(); char ch = gc(); while (isspace(ch)) ch = gc(); while (!isspace(ch) && ch != EOF) { x.push_back(ch); ch = gc(); } }
    inline void printstr(char* x) { for (int i = 0; i < (int)strlen(x); i++) pc(x[i]); }
    inline void printstr(string& x) { for (auto i = x.begin(); i != x.end(); i++) pc(*i); }
};
using namespace IO;

#define OUT 0
#define MAMBA return
typedef long long ll;
typedef long double db;
const int N = 1e5 + 10;
const int INF = 0x3f3f3f3f;
int man();int main(){MAMBA man();}
inline int _abs(int a) { if (a < 0) return -a; return a; }
inline int _pow(int a, int b) { int x = 1, y = a; while(b > 0) {if (b & 1) x *= y; y *= y; b >>= 1; } return x; }
// ----------------------------
struct node {
    string name;
    int score, id;
};
// ----------------------------
node members[N];
unordered_map<string, node> mp;
// ----------------------------
bool cmp(node a, node b) {
    if (a.score != b.score) return a.score > b.score;
    return a.id < b.id;
}

int man() {
    int n; read(n);
    // ----------------------------
    int score;
    string name;
    for (int i = 1; i <= n; i++) {
        readstr(name); read(score);
        mp[name].id = i;
        mp[name].score += score;
    }
    int cnt = 0;
    for (auto i : mp) members[++cnt] = {i.first, i.second.score, i.second.id};
    sort(members + 1, members + cnt + 1, cmp);
    // ----------------------------
    printstr(members[1].name);
    MAMBA OUT;
}
/*
                 .-~~~~~~~~~-._       _.-~~~~~~~~~-.
             __.'              ~.   .~              `.__
           .'//   A    C    之   \./  之    真    理  \`.
         .'//                     |                     \`.
       .'// .-~"""""""~~~~-._     |     _,-~~~~"""""""~-. \`.
     .'//.-"                 `-.  |  .-'                 "-.\`.
   .'//______.============-..   \ | /   ..-============.______\`.
 .'______________________________\|/______________________________`.
*/