P1963 [NOI2009]变换序列 倒叙跑匈牙利算法

题意

构造一个字典序最小的序列T,使得 Dis(i, Ti) = di,其中i是从0开始的,Dis(x,y)=min{∣x−y∣,N−∣x−y∣} ,di由题目给定。

思路

二分图匹配,把左边的看成i,右边看成Ti,对于固定的i和d,Ti是由两种可能的,连上有向边即可。
至于字典序要最小,怎么做呢,我们可以反着跑匈牙利算法,就是从n-1跑到0,这样小一点的i,可以直接拿走大一点的i刚匹配的较小的值。

#include <algorithm>
#include  <iterator>
#include  <iostream>
#include   <cstring>
#include   <cstdlib>
#include   <iomanip>
#include    <bitset>
#include    <cctype>
#include    <cstdio>
#include    <string>
#include    <vector>
#include     <stack>
#include     <cmath>
#include     <queue>
#include      <list>
#include       <map>
#include       <set>
#include   <cassert>

/*
        
⊂_ヽ
  \\ Λ_Λ  来了老弟
   \('ㅅ')
    > ⌒ヽ
   /   へ\
   /  / \\
   レ ノ   ヽ_つ
  / /
  / /|
 ( (ヽ
 | |、\
 | 丿 \ ⌒)
 | |  ) /
'ノ )  Lノ

*/

using namespace std;
#define lson (l , mid , rt << 1)
#define rson (mid + 1 , r , rt << 1 | 1)
#define debug(x) cerr << #x << " = " << x << "\n";
#define pb push_back
#define pq priority_queue



typedef long long ll;
typedef unsigned long long ull;
//typedef __int128 bll;
typedef pair<ll ,ll > pll;
typedef pair<int ,int > pii;
typedef pair<int,pii> p3;

//priority_queue<int> q;//这是一个大根堆q
//priority_queue<int,vector<int>,greater<int> >q;//这是一个小根堆q
#define fi first
#define se second
//#define endl '\n'

#define boost ios::sync_with_stdio(false);cin.tie(0)
#define rep(a, b, c) for(int a = (b); a <= (c); ++ a)
#define max3(a,b,c) max(max(a,b), c);
#define min3(a,b,c) min(min(a,b), c);


const ll oo = 1ll<<17;
const ll mos = 0x7FFFFFFF;  //2147483647
const ll nmos = 0x80000000;  //-2147483648
const int inf = 0x3f3f3f3f;
const ll inff = 0x3f3f3f3f3f3f3f3f; //18
const int mod = 1e9+7;
const double esp = 1e-8;
const double PI=acos(-1.0);
const double PHI=0.61803399;    //黄金分割点
const double tPHI=0.38196601;


template<typename T>
inline T read(T&x){
    x=0;int f=0;char ch=getchar();
    while (ch<'0'||ch>'9') f|=(ch=='-'),ch=getchar();
    while (ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
    return x=f?-x:x;
}
inline void cmax(int &x,int y){if(x<y)x=y;}
inline void cmax(ll &x,ll y){if(x<y)x=y;}
inline void cmin(int &x,int y){if(x>y)x=y;}
inline void cmin(ll &x,ll y){if(x>y)x=y;}

/*-----------------------showtime----------------------*/
 
            const int maxn = 1e4+9;
            struct E{
                int v,nxt;
            }edge[4*maxn];
            int head[maxn],gtot;
            void addedge(int u,int v){
                edge[gtot].v = v;
                edge[gtot].nxt = head[u];
                head[u] = gtot++;
            }
            int vis[maxn],pt[maxn],py[maxn];
            bool dfs(int u){
                for(int i=head[u]; ~i; i = edge[i].nxt){
                    int v = edge[i].v;
                    if(vis[v] == 0){
                        vis[v] = 1;
                        if(pt[v] == 0 || dfs(pt[v])){
                            pt[v] = u;
                            py[u] = v;
                            return true;
                        }
                    }
                }
                return false;
            }
int main(){
            int n;
            scanf("%d", &n);
            memset(head, -1, sizeof(head));
            priority_queue<int>que;
            while(!que.empty()) que.pop();
            for(int i=0; i<n; i++){
                int d;  scanf("%d", &d);
                int a = i-d;

                if(a>=0&&a<n)  que.push(a);
                a = i+d;
                if(a>=0&&a<n)  que.push(a);
                int b = i - (n-d);
                if(b>=0&&b<n)  que.push(b);
                b = i + (n-d);
                if(b>=0&&b<n)  que.push(b);
                while(!que.empty()){
                    int t = que.top(); que.pop();
                    addedge(i, t);
                }
            }
            int res = n;
            for(int i=n-1; i>=0; i--){
                memset(vis, 0, sizeof(vis));
                if(dfs(i)) res = i;
                else break;
            }
            if(res == 0) {
                for(int i=0; i<n; i++) printf("%d ", py[i]);
                puts("");
            }
            else puts("No Answer");
            return 0;
}
View Code

猜你喜欢

转载自www.cnblogs.com/ckxkexing/p/10393509.html