跳到正文

题解

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

gcx114514约 4 分钟
原文
文章信息与版本

文章信息

发布
2025年1月22日
收录
2025年12月12日
更新
2026年11月21日
篇幅
约 4 分钟
获赞
7
题目
P11890

版本

  1. v1当前

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

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

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

Code

C++
#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;
}