题解:P13849 [CERC 2023] Equal Schedules

· · 题解

说实话,这读入真恶心...

读入用 C++ 的 string 类型会更方便。

其实此题可用 \text{std::map} 存储

思路:

  1. 先读入一个字符串 a ,若 a=="------" 结束读入。否则执行 2
  2. 读入一个整数和一个字符串,将 a 转化为数字,并令相对应的 map[str] 减去两个整数之差。

再次读入,但这次让 map[str] 加上两个整数之差。

然后,利用 map 自动给数组关键字排序的特性,用迭代器遍历 map,判断是否为 0,并标记。

最后,若没有输出(即标记未改变),输出 No differences found.

代码:

#include<bits/stdc++.h>
using namespace std;
#define f first
#define s second
map<string,int>mp;
int main()
{
    string a,c;
    int b;
    while(cin>>a,a!="------")//读入很重要!!!
    {
        cin>>b>>c;
        int x=0;
        for(char i:a)
            x=x*10+i-'0';
        mp[c]-=b-x;
    }
    a=c="";
    while(cin>>a,a!="======")
    {
        cin>>b>>c;
        int x=0;
        for(char i:a)
            x=x*10+i-'0';
        mp[c]+=b-x;
    }
    bool k=false;
    map<string,int>::iterator it=mp.begin();
    while(it!=mp.end())
    {
        if((*it).s<0)
        {
            cout<<(*it).f<<" "<<(*it).s<<'\n';
            k=true;
        }
        else if((*it).s>0)
        {
            cout<<(*it).f<<" +"<<(*it).s<<'\n';
            k=true;
        }
        it++;
    }
    if(!k)
    cout<<"No differences found.";
    return 0;
}