题解 - T763135

· · 个人记录

前言

出题人题解。

比赛链接

其实……

应该算个 \color{#FFC116}普及/提高-。

思路

首先能想到骗分,是 O(n^2),能拿 60 分。

正解有 2 种方法,想看哪个看哪个。

::::info[法一:队列] 这是出题人写的,想法是维护第一个没炒的人在队首。

维护就用一个 map 记录当前的那一种面在有几个人的时候炒的。

之后应该搞完了吧……

不懂的可以结合代码观看。

:::success[代码] 如果你没通过,应该是酷丁评测机有点慢,可以用 getchar_unlocked 替换 getchar,或者加点卡常。

#include<iostream>
#include<vector>
#include<queue>
#include<map>
#define int long long
using namespace std;
typedef pair<int,int> P;
queue<P> q;
vector<int> ans[1000005];
int n,pos,posans;
map<long long,int> t,t_ans;
int read(){
    int x=0,f=1;
    char c=getchar();
    for(;c<'0'||c>'9';c=getchar()) if(c=='-') f=-1;
    for(;c>='0'&&c<='9';c=getchar()) x=x*10+c-'0';
    return x*f;
}
void write(int x){
    if(x<0) putchar('-'),x=-x;
    if(x<10) putchar(x%10+'0');
    else write(x/10),putchar(x%10+'0');
}
signed main(){
    n=read();
    for(int i=1,op,x;i<=n;i++){
        op=read();
        if(op==0){
            posans++;
            if(q.empty()) continue;
            x=q.front().first;
            t_ans[x]=posans;
            t[x]=pos;
            while(!q.empty()){
                x=q.front().first;
                if(t[x]>=q.front().second){
                    ans[t_ans[x]].push_back(q.front().second);
                    q.pop();
                }else{
                    break;
                }
            }
        }else{
            x=read();
            q.push({x,++pos});
        }
    }
    int x;
    while(!q.empty()){
        x=q.front().first;
        if(t[x]>=q.front().second){
            ans[t_ans[x]].push_back(q.front().second);
        }
        q.pop();
    }
    for(int i=1;i<=posans;i++){
        for(int j:ans[i]){
            write(j);
            putchar(' ');
        }
        putchar('\n');
    }
    return 0;
}
:::
::::info[法二:离散化]
正常你会用 vector 存每种面对应的客人对不对?

但是 1 \le k \le 10^{12},空间直接被炸飞,但 n 最多是 10^6,也就意味着最多有 10^6 个客人,完全可以离散化,之后再按用 vector 存储的思路就可以了。

sort 有风险,使用需谨慎,可能会被炸飞。

如果你两个都用应该可以压缩到 O(n),但一般人想不出来。(用基数排序)