1. 程式人生 > >P1963 [NOI2009]變換序列 倒敘跑匈牙利算法

P1963 [NOI2009]變換序列 倒敘跑匈牙利算法

onclick alt mos con back 小根堆 == 匹配 tor

題意

構造一個字典序最小的序列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

P1963 [NOI2009]變換序列 倒敘跑匈牙利算法