跳到正文

题解

题解:P15217 [NWERC 2025] Last Christmas

ycy25约 2 分钟
原文
文章信息与版本

文章信息

发布
2026年2月9日
收录
2026年6月14日
更新
2026年11月18日
篇幅
约 2 分钟
获赞
2
题目
P15217

版本

  1. v1当前

map 真是太好用辣!

思路

开一个结构体记录选手拿到第 11 至 1010 名分别的次数,使用 map 记录每名选手,最后遍历一遍,判断最佳艺术家是谁,有多名输出 tie。

代码

C++
#include<bits/stdc++.h>
using namespace std;
struct Jgt
{
    int s[10];
};
bool operator<(Jgt a,Jgt b)//重载运算符
{
    int suma=0,sumb=0;
    for(int i=0;i<10;i++){suma+=a.s[i],sumb+=b.s[i];}
    if(suma<sumb)return true;
    else if(suma>sumb)return false;//优先比较谁出现次数最高
    for(int i=0;i<10;i++)
    {
        if(a.s[i]<b.s[i])return true;//从获得第1名的次数开始依次比较
        else if(a.s[i]>b.s[i])return false;
    }
    return false;
}
bool operator==(Jgt a,Jgt b)
{
    for(int i=0;i<10;i++)
    {
        if(a.s[i]!=b.s[i])return false;
    }
    return true;
}
map<string,Jgt>p;
int main()
{
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);//关闭同步输入输出流
    int n;
    cin>>n;
    string t;
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=10;j++)
        {
            cin>>t;
            p[t].s[j-1]++;名字为t的选手获得第j名的次数加一,注意下标从0开始
        }
    }
    bool tie=false;
    for(auto [name,pz]:p)
    {
        if(p[t]<pz)
        {
            t=name;
            tie=false;
        }
        else if(p[t]==pz&&t!=name)tie=true;//有相同的则可能平局
    }
    cout<<(tie?"tie":t);//三目运算符
}
原文