Topic
Input format
Output format
Input sample
5
1 1 2) 2 1
Output sample
1 2 4) 0 3
Tips
30% of the data are n≤50;
60% of the data are n≤500;
100% of the data are n≤10000.
Exercises
Each position can be matched to two numbers, which is obviously a binary graph matching problem
But the dictionary order is minimal, and we're keeping it by dictionary.
Because in the KM algorithm, later with a higher priority, we can match in reverse order
#include <iostream>#include <cstdio>#include <cmath>#include <vector>#include <cstring>#include <algorithm>#define LL Long Long int#define Redge (U) for (int k = h[u],to; k; k = ed[k].nxt)#define REP (i,n) for (int i = 1; I <= (n); i++)#define BUG (s,n) for (int i = 1; I <= (n); i++) cout<<s[i]<< '; puts ("");using namespaceStdConst intMAXN =10005, MAXM =100005, INF =1000000000;inline intRead () {intout =0, flag =1;Charc = GetChar (); while(C < -|| C > $){if(c = ='-') flag =-1; c = GetChar ();} while(c >= -&& C <= $) {out = (out <<3) + (out <<1) + C- -; c = GetChar ();}returnOut * FLAG;}intLK[MAXN],VIS[MAXN],ANS[MAXN];intH[maxn],ne =1;structedge{intTO,NXT;} ED[MAXN <<1];inline voidBuildintUintV) {Ed[ne] = (EDGE) {v,h[u]}; h[u] = ne++;}BOOLFindintu) {redge (U)if(!vis[to = ed[k].to]) {Vis[to] =true;if(Lk[to] = =-1|| Find (Lk[to])) {lk[to] = u;return true; } }return false;}intN,A[MAXN],B[MAXN];intMain () {memset (LK,-1,sizeof(LK)); n = read ();intTmp,a,b; for(inti =0; I < n; i++) {tmp = read (); A = (i + tmp)% n; B = (i-tmp + N)% n;if(A < b) Swap (A, a); Build (I,a); Build (I,B); } for(inti = n-1; I >=0; i--) {memset (Vis,0,sizeof(VIS));if(!find (i)) {Puts ("No Answer");return 0; } } for(inti =0; I < n; i++) Ans[lk[i]] = i; for(inti =0; I < n; i++) {printf ("%d", Ans[i]);if(I < n-1) printf (" "); }return 0;}
BZOJ1562 [NOI2009] transform sequence "km algorithm"