# 洛谷1963 [noi2009]变换序列

## 匈牙利算法的本质

Posted by yjjr's blog on October 29, 2018

# 题目

## 输入输出样例

### 输入样例#1

5
1 1 2 2 1


### 输出样例#1

1 2 4 0 3


# code

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define dep(i,a,b) for(int i=a;i>=b;i--)
#define ll long long
#define mem(x,num) memset(x,num,sizeof x)
#define reg(x) for(int i=last[x];i;i=e[i].next)
using namespace std;
ll f=1,x=0;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
const int maxn=1e4+6;
int n,d[maxn],mt[maxn],ans[maxn],last[maxn<<2],cnt=0;bool vis[maxn];
inline int dis(int x,int y){return min(abs(x-y),n-abs(x-y));}
vector <int> e[maxn];
inline bool dfs(int x){
for(int i=0;i<e[x].size();i++)
if(!vis[e[x][i]]){
vis[e[x][i]]=1;
if(mt[e[x][i]]==0||dfs(mt[e[x][i]])){mt[e[x][i]]=x;return 1;}
}
return 0;
}
int main(){
rep(i,0,n-1){
int x=i+d[i],y=i-d[i]+n;
x%=n,y%=n;
if(dis(x,i)!=d[i])x=-1;
if(dis(y,i)!=d[i])y=-1;
if(x>y)swap(x,y);
if(x!=-1)e[i].push_back(x);
if(y!=-1)e[i].push_back(y);
}
dep(i,n-1,0){
mem(vis,0);