JZOJ5891. 【NOIP2018模拟10.4】巡逻

在这里插入图片描述
在这里插入图片描述

题解

每次贪心,将最小的放在最前面,
将那些没有条件限制的所有数,一定是从小到大来。
于是每次就要么从有限制的那些数里面去一个,
或者从没有限制的数里面去最小的。

code

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#define N 100003
#define P putchar
#define G getchar
using namespace std;
char ch;
void read(int &n)
{
	n=0;
	ch=G();
	while((ch<'0' || ch>'9') && ch!='-')ch=G();
	int w=1;
	if(ch=='-')w=-1,ch=G();
	while('0'<=ch && ch<='9')n=(n<<3)+(n<<1)+ch-'0',ch=G();
	n*=w;
}

void write(int x){if(x>9) write(x/10);P(x%10+'0');}

int n,m,a[N],b[N],p,mm;
bool bz[N];

int main()
{
	freopen("patrol.in","r",stdin);
	freopen("patrol.out","w",stdout);
	
	memset(bz,1,sizeof(bz));
	read(n);read(m);
	for(int i=1;i<=m;i++)read(a[i]),bz[a[i]]=0;
	for(int i=1;i<=n;i++)
		if(bz[i])b[++mm]=i;
	p=1;
	for(int i=1;i<=m;i++)
	{
		for(;p<=mm && b[p]<a[i];p++)write(b[p]),P('\n');
		write(a[i]),P('\n');
	}
	for(;p<=mm;p++)write(b[p]),P('\n');
	
	return 0;
}

猜你喜欢

转载自blog.csdn.net/lijf2001/article/details/82940460