[NOI2009]变换序列

Description
1562_1.jpg

Input
1562_2.jpg

Output
1562_3.jpg

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;
}

转载于:https://www.cnblogs.com/Wolfycz/p/10251425.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值