Codeforces 2A Winner(map) (*1600)

本文分享了在CodeForces竞赛中遇到的一道难题解决经验,通过使用map数据结构进行一一映射,解决了题目中的复杂逻辑问题。文章详细记录了从初见题目时的轻敌到陷入死循环的困扰,再到最终找到正确解题思路的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

轻敌了,想得太简单,后来脑子短路被困在了死循环里,捣鼓了一阵

用map一一映射保存结果,注意中间过程也要保存,被题干坑了一次,以后一定先认真审题然后敲

一开始没想明白,直接做复杂了。。

http://codeforces.com/problemset/page/47

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<set>
#include<map>
#include<queue>
#include<cmath>
#define ll long long
#define mod 1000000007
#define inf 0x3f3f3f3f
using namespace std;
struct node
{
    string s;
    int x;
}a[1005];
int main()
{
    int n;
    scanf("%d",&n);
    map<string, int> m;
    map<string, bool> bol;
    string name[1005];
    int cnt = 0;
    int maxy = -inf;
    string s;
    int tmp;
    for(int i = 1; i <= n; i ++)
    {
        cin>>s>>tmp;
        m[s] += tmp;
        if(! bol[s])
        {
            name[++ cnt] = s;
            bol[s] = 1;
        }
        a[i].s = s;
        a[i].x = m[s];
    }
    maxy = -inf;
    string ans1;
    for(int i = 1; i <= cnt; i ++)
    {
        if(maxy < m[name[i]])
        {
            ans1 = name[i];
            maxy = m[name[i]];
        }
    }
    //int cnt1 = 0;
    map<string, bool> ans;
    for(int i = 1; i <= cnt; i ++)
    {
        if(m[name[i]] == maxy)
        {
            ans[name[i]] = 1;
        }
    }
    for(int i = 1; i <= n; i ++)
    {
        if(ans[a[i].s])
        {
            if(a[i].x >= maxy)
            {
                cout<<a[i].s<<endl;
                break;
            }
        }
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值