【STL】codeforces 818D Multicolored Cars

来源:互联网 发布:幽浮2知乎 编辑:程序博客网 时间:2024/05/29 19:30

Link:http://codeforces.com/problemset/problem/818/D

#include <bits/stdc++.h>using namespace std;typedef long long LL;/*题意:在 1 到 n 时刻,有 n 量有颜色的车通过,用数字表示颜色,Alice 选择一个颜色A,要求 Bob 选择一个颜色B,使得对于任意时刻 cnt(B) >= cnt(A),即通过的颜色为 B 的车始终不小于颜色为 A 的车。求任意满足条件的解,否则输出 -1 。题解:将出现A颜色之前的颜色以及出现的次数在集合里存起来,到A颜色的时候全部减去1,当减成-1时,推出集合,永远不再加入集合,处理到最后,集合里的元素都是可以的*/const int Maxn = 1e6+6;set<int> set1,set2;int num[Maxn],ff[Maxn];int main(){    int n,a;    scanf("%d%d",&n,&a);    int f = 0;    for(int i = 0; i < n; i++)    {        int x;        scanf("%d",&x);        if(x != a)        {            if(!f || set2.count(x))            {                set1.insert(x);                num[x]++;            }        }        else        {            f = 1;            int k = 0;            set<int>::iterator it;            for(it = set2.begin(); it != set2.end(); it++)            {                num[*it]--;                if(num[*it] == 0)                    ff[k++] = *it;            }            for(int i = 0; i < k; i++)                set2.erase(ff[i]);            set2.insert(set1.begin(),set1.end());            set1.clear();        }    }    if(!f)    {        printf("%d\n",*set1.begin());        return 0;    }    if(set2.empty())        puts("-1");    else        printf("%d\n",*set2.begin());    return 0;}