BZOJ1562 [NOI2009] transform sequence "km algorithm"

Source: Internet
Author: User

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"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.