题解:[XRCOI Round 1] 相聚相逢本无意

· · 题解

首先我们先特判掉两种情况,一种是 \text{MEX} 限制不为 0 的只有 0 的,我们直接输出限制个极大值即可。另一种是 \text{MEX} 限制不为零的除了 0 还要别的数则一定不合法,因为要满足序列单调不降。

判断完这两种情况,我们考虑计算其他情况。由于要求 a 单调不降,所以将输入序列按 x 升序排列是显然的,当然,我们需要过滤掉末尾连续的 y 为 0 的限制,因为我们完全可以不去遍历。然后这样遍历限制,同时维护一个 \text{MEX} 值,每次判断当前扫到的要求是不是小于 0 即可。

(与出题人讨论了若干种细节,给出题人弄红温了。)

Code

#include <bits/stdc++.h>
#define pii pair<int,int>
#define ll long long
#define mk make_pair
#define reaD read
#define raed read
#define haed head
#define cotu cout
#define se second
#define fi first
#define itn int
using namespace std;
bool Mst;
const int Max=2e5+10;
const int mod=998244353;
const int inf=1e9+10;


inline int read(){
    int res=0,v=1;
    char c=getchar();
    while(c<'0'||c>'9'){v=(c=='-'?-1:1);c=getchar();}
    while(c>='0'&&c<='9'){res=(res<<3)+(res<<1)+(c^48);c=getchar();}
    return res*v;
}

struct po{
    int x,y;
    po(int x=0,int y=0):
        x(x),y(y){;}
    bool operator <(const po a)const{
        return x<a.x;
    }
}a[Max];

vector<int>ans;

bool Med;
signed main(){
    int n=read();
    int z=0;
    for(int i=1;i<=n;++i){
        int x,y;
        x=read();
        y=read();
        if(y!=0)++z;
        a[i]=po(x,y);
    }
    sort(a+1,a+1+n);
    int j=0;
    if(a[1].x==0&&a[1].y!=0&&z==1){
        cout << a[1].y << '\n';
        for(int i=1;i<=a[1].y;++i){
            cout << "100000 ";
        }
        return 0;
    }
    if(a[1].x==0&&a[1].y>0&&z>1){
        cout << "-1\n";
        return 0;
    }
    for(int i=1;i<=n;++i){
        int z=0;
        for(int j=i;j<=n;++j){
            if(a[j].y){
                z=1;
                break;
            }
        }
        if(!z)break;
        if(j!=a[i].x){
            while(j<a[i].x){
                ans.push_back(j);
                ++j;
            }
            --a[i].y;
        }
        if(a[i].y<0){
            cout << "-1\n";
            return 0;
        }
        while(a[i].y){
            ans.push_back(j-1);
            --a[i].y;
        }
    }
    cout << ans.size() << "\n";
    for(int i:ans){
        cout << i << ' ';
    }
    cout << "\n";

    cerr<< "Time: "<<clock()/1000.0 << "s\n";
    cerr<< "Memory: " << (&Mst-&Med)/1000000.0 << "MB\n";
    return 0;
}