Title: http://cojs.tk/cogs/problem/problem.php?pid=409409. [NOI2009] Transform sequence
★★☆ input file: transform.in
output file: transform.out
Simple comparison
Time limit: 1 s memory limit: MB
"Problem description"
For
NAn integer 0, 1, ...,
N-1, a transform sequence
Tcan add
IBecome
Ti, which defines
xAnd
yThe distance between them. Given each
IAnd
TiThe distance between
D(
I,
Ti), you need to request a transformation sequence that satisfies the requirements
T。 If there is more than one sequence that satisfies the condition, the output is one of the smallest dictionary order.
Description: For two transform sequences
SAnd
T, if there is
P<
N, Meet for
I=0,1,......
P-1,
Si=
TiAnd
Sp<
Tp, we call
SThan
TSmall dictionary order.
"Input File"The first line of the input file transform.in contains an integer
NThat represents the length of the sequence. The next line contains
NAn integer
Di, where
DiSaid
IAnd
TiThe distance between them.
"Output File"The output file is transform.out. If there is at least one transformation sequence that satisfies the requirement
T, a row is included in the output file
NInteger that represents the smallest dictionary order you have computed.
TOtherwise output "no Answer" (without quotation marks).Note: The two adjacent numbers in the output file are separated by a space, and the end of the line does not contain extra spaces.
"Input Sample"51 1 2) 2 1
"output Example"1 2 4) 0 3
"Data size and conventions"In 20% of the data
NIn the ≤50;60% data
NIn the ≤500;100% data
N≤10000. : Binary map matching + Hungarian algorithm set the original sequence value to x[0],x[1],x[2]......x[n-1], the sequence value to become y[0],y[1],y[2]......y[n-1]. The original sequence is 0,1,2......n-1, That is x[0]=0,x[1]=1,x[2]=2......x[n-1]=n-1. First, the sample can be thought of each value x[i] can become the value of the y[i] is only two, but why, the proof is as follows: set a=| X[i]-y[i] |, and A ≤ (N-A), where A, (n-a) are the difference (known by the topic) for each x[i], we have y[i]=x[i]±a or y[i]=x[i]± (n-a) . because each x[i] and y [i] to satisfy the 0≤x[i]≤n-1,0≤y[i]≤n-1, and Y[i]=x[i]±a or y[i]=x[i]± (n-a) . can be obtained: x[i]+a≤n-1 -------------- (1) type x[i]-a≥0 ------------ -- (2) type x[i]+ (n-a) ≤n-1 ---------- (3) type x[i]-(n-a) ≥0 ------------- (4 ) type (3) simplification can be: x[i]-a≤-1 (4) Type of movement can be: x[i]+a≥n observe four formulas, we can find: (1) and (4) is contradictory, (2) and (3) is contradictory . so, We can only choose one in (1) and (4), (2) and (3) to choose a . . then, with this conclusion, we turn the topic into: GiveOut of the 0...n-1, the amount of each number of changes (the amount of change to the absolute value, that is, the difference can be changed), each number must be changed and just change the given difference, each number can be changed to two number (it has been proved), let you become 0...n-1 and make the sequence of the Order of the dictionary is minimal. You can run Hungary. (Time complexity of the puzzle ...) In fact, it is to judge whether
Perfect Match。 If not
Perfect Match, the output has no solution. Note that Hungary is running backwards and can run out of the dictionary order.
1#include <bits/stdc++.h>2 using namespacestd;3 #defineMAXN 100104 intbf[maxn],bf1[maxn],d[maxn],prey[maxn],n,f[maxn][2];5 //bool F[MAXN][MAXN];6Bitset<maxn>Vis;7 intRead ()8 {9 ints=0, fh=1;CharCh=GetChar ();Ten while(ch<'0'|| Ch>'9'){if(ch=='-') fh=-1; ch=GetChar ();} One while(ch>='0'&&ch<='9') {s=s*Ten+ (ch-'0'); ch=GetChar ();} A returns*fh; - } - intXyl (intu) the { - inti,v; - for(i=0; i<=1; i++) - { +v=F[u][i]; - if(vis[v]==0) + { Avis[v]=1; at if(Xyl (bf[v]) = =1|| bf[v]==0) - { -bf[v]=u; -bf1[u]=v; - return 1; - } in } - } to return 0; + } - intMain () the { *Freopen ("transform.in","R", stdin); $Freopen ("Transform.out","W", stdout);Panax Notoginseng intI,ans,dd,gs; -n=read (); theMemset (F,false,sizeof(f)); + for(i=0; i<n;i++) A { theD[i]=read ();dd =n-D[i]; +gs=-1; - if(i-d[i]>=0) f[i][++gs]=i-D[i]; $ if(i+d[i]<=n-1) f[i][++gs]=i+D[i]; $ if(i-dd>=0) f[i][++gs]=i-DD; - if(i+dd<=n-1) f[i][++gs]=i+DD; - if(f[i][0]>=f[i][1]) Swap (f[i][0],f[i][1]); the } -memset (BF,0,sizeof(BF)); ans=0;Wuyimemset (prey,-1,sizeof(Prey)); the for(i=n-1; i>=0; i--) - { Wu Vis.reset (); -ans+=Xyl (i); About //Prey[i]=bf1[i]; $ } - if(ans!=n) printf ("No Answer"); - Else - { A //For (i=0;i<n;i++) bf1[bf[i]]=i; + for(i=0; i<n;i++) printf ("%d", Bf1[i]); the } - fclose (stdin); $ fclose (stdout); the return 0; the}
Bzoj 1562: [NOI2009] Conversion sequence Hungarian algorithm, binary graph matching