[NOI2009]变换序列

Description

Input

Output

Sample Input
5
1 1 2 2 1

Sample Output
1 2 4 0 3

HINT
30%的数据中N≤50;
60%的数据中N≤500;
100%的数据中N≤10000。


对于每个点,向它应该放置的位置连一条边,然后跑一遍匈牙利,连边的时候记得考虑顺序,使得字典序最小;然后匈牙利算法匹配的时候,会优先考虑当前点,腾出其他点的空间,于是我们倒序枚举即可

/*program from Wolfycz*/
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define inf 0x7f7f7f7f
using namespace std;
typedef long long ll;
typedef unsigned int ui;
typedef unsigned long long ull;
inline char gc(){
	static char buf[1000000],*p1=buf,*p2=buf;
	return p1==p2&&(p2=(p1=buf)+fread(buf,1,1000000,stdin),p1==p2)?EOF:*p1++;
}
inline int frd(){
	int x=0,f=1; char ch=gc();
	for (;ch<'0'||ch>'9';ch=gc())	if (ch=='-')	f=-1;
	for (;ch>='0'&&ch<='9';ch=gc())	x=(x<<3)+(x<<1)+ch-'0';
	return x*f;
}
inline int read(){
	int x=0,f=1; char ch=getchar();
	for (;ch<'0'||ch>'9';ch=getchar())	if (ch=='-')	f=-1;
	for (;ch>='0'&&ch<='9';ch=getchar())	x=(x<<3)+(x<<1)+ch-'0';
	return x*f;
}
inline void print(int x){
	if (x<0)	putchar('-'),x=-x;
	if (x>9)	print(x/10);
	putchar(x%10+'0');
}
const int N=1e4;
int path[N+10],vis[N+10],Ans[N+10],f[2][N+10],Time;
bool Extra(int x){
	for (int i=0;i<2;i++){
		int son=f[i][x];
		if (vis[son]==Time)	continue;
		vis[son]=Time;
		if (!~path[son]||Extra(path[son])){
			path[son]=x;
			return 1;
		}
	}
	return 0;
}
int main(){
	memset(path,255,sizeof(path));
	int n=read(),res=0;
	for (int i=0;i<n;i++){
		int tmp=read(),x=(i-tmp+n)%n,y=(i+tmp)%n;
		if (x>y)	swap(x,y);
		f[0][i]=x,f[1][i]=y;
	}
	for (int i=n-1;~i;i--){
		++Time;
		if (Extra(i))	res++;
	}
	if (res!=n){
		printf("No Answer\n");
		return 0;
	}
	for (int i=0;i<n;i++)	Ans[path[i]]=i;
	for (int i=0;i<n;i++)	printf("%d",Ans[i]),putchar(i==n-1?'\n':' ');
	return 0;
}
posted @ 2019-01-10 17:45  Wolfycz  阅读(166)  评论(0编辑  收藏  举报