Topic Links:
http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1807
Main topic:
Give you a series of length N (n<=105), 0 of the series can be replaced by other numbers, and eventually form a 1~n arrangement, the longest ascending subsequence of this arrangement is the length of the N-1 scheme number.
Topic Ideas:
Simulation
This problem needs to be discussed in a classified way.
First, it is certain that a sequence of length n is the longest ascending sequence length of n-1 (longest descent subsequence length of 2), then the appearance of this sequence is 1~n from small to large after one of the numbers moved to the middle of the rest of the number (dislocation)
A 0 interval of length l, consider the number I, if placed on position I, then f[i]=f[i-1]
If placed on the i-1 position, then the I-1 method (the number of the front i-1 can be placed in the first position, at this time dislocation of the first position on the numbers)
If placed in other locations, then this number is the number of dislocation, then the number can be placed in a position of i-2 (the other number must be arranged from small to large to ensure that the number of dislocation is 1)
Now consider the given number, if a number a[i] and I offset more than 1, then it means that it must have at least 2 larger than it (a[i]-i>1), or behind it there are at least 2 smaller than it (a[i]-i<-1), then the current a[i] Must be the only final answer in the number of offsets, or no solution.
If the given number is not offset, then the number of that offset must be one in the interval of only 0, then Ans=σf[l] (f[l) is the number of scenarios with 0 interval offset 1 for length L)
If the given number in the interval [l,r] is shifted forward by 1, then there is a number less than L at the back of R (if the given number in the interval [l,r] is shifted backwards by 1, representing a number greater than R in front of L), and according to the previous judgement the number has not been filled (the offset is more than 1), The left end is less than L's =l number of 0 (L[l]) between the right and the first a[i]=i, and the ability to fill is 0 (r[r]) between R and the first a[i]=i, so Ans=l[l]*r[r]. (another case in the same way)
There is also a situation where there are two digital exchange positions, which I thought I was leaking from the beginning. The two-bit digital exchange only works when the two digits are adjacent and only has a unique solution, and requires that other numbers must be arranged from small to large to be misaligned.
1 //2 //by Coolxxx3 //#include <bits/stdc++.h>4#include <iostream>5#include <algorithm>6#include <string>7#include <iomanip>8#include <map>9#include <stack>Ten#include <queue> One#include <Set> A#include <bitset> -#include <memory.h> -#include <time.h> the#include <stdio.h> -#include <stdlib.h> -#include <string.h> - //#include <stdbool.h> +#include <math.h> - #defineMin (a) < (b) ( A):(B)) + #defineMax (a) (a) > (b)? ( A):(B)) A #defineABS (a) ((a) >0? ( A):(-(a))) at #defineLowbit (a) (a& (a)) - #defineSqr (a) ((a) * (a)) - #defineSwap (a) (a) ^= (b), (b) ^= (a), (a) ^= (b)) - #defineMem (A, B) memset (A,b,sizeof (a)) - #defineEPS (1E-10) - #defineJ 10000 in #defineMoD 1000000007 - #defineMAX 0x7f7f7f7f to #definePI 3.14159265358979323 + #pragmaComment (linker, "/stack:1024000000,1024000000") - #defineN 100004 the using namespacestd; *typedefLong LongLL; $ DoubleAnss;Panax Notoginseng LL Aans; - intCas,cass; the intN,m,lll,ans; + intA[n]; A LL F[n]; the voidInit () + { - inti; $f[0]=f[1]=0; $ for(i=2; i<n;i++) f[i]=f[i-1]+i-1+i-2; - } - voidWORK1 (intx) the { - inti,t=1;Wuyi for(i=1; i<=n;i++) the { - if(i==x)Continue; Wu if(T==a[x]) t++; - if(T==a[i] | |!a[i]) t++; About Else Break; $ } - if(I<=n) puts ("0"); - ElsePuts"1"); - } A voidWORK2 (intLintR) + { the intI,x,ll=0, rr=0; - for(i=l;i<=r;i++) $ { the if(!a[i])Continue; the if(a[i]-i!=a[l]-l) Break; the } the if(I<=R) {Puts ("0");return;} - for(i=l-1; i;i--) in { the if(!a[i]) ll++; the Else if(a[i]-i!=a[l]-l) Break; About } the for(i=r+1; i<=n;i++) the { the if(!a[i]) rr++; + Else if(a[i]-i!=a[l]-l) Break; - } the if(a[l]-l==1) ll++;Bayi Elserr++; theprintf"%lld\n", 1ll*ll*RR); the } - voidWORK3 (intXintYintLintR) - { the inti; the if(x!=y | | l!=r | | x!=l-1) {puts ("0");return;} thePuts"1"); the } - voidWork4 () the { the inti,sz=0; the for(i=1; i<=n;i++)94 { the if(!a[i]) sz++; the Elseaans+=f[sz],sz=0; the }98 if(SZ) aans+=F[sz]; Aboutprintf"%lld\n", Aans); - }101 intMain ()102 {103 #ifndef Online_judge104 //freopen ("1.txt", "R", stdin); the //freopen ("2.txt", "w", stdout);106 #endif107 inti,j,k;108 intX,y,l,r;109 init (); the //for (scanf ("%d", &cass); cass;cass--)111 //for (scanf ("%d", &cas), cass=1;cass<=cas;cass++) the //while (~scanf ("%s", s))113 while(~SCANF ("%d",&N)) the { theaans=0; thex=l=n+1, y=r=0;117 for(i=1; i<=n;i++) scanf ("%d", A +i);118 for(i=1; i<=n;i++)119 { - if(!a[i])Continue;121 if(ABS (A[I]-I) >1) {Work1 (i); Break;}122 if(a[i]-i==1) X=min (x,i), y=Max (y,i);123 if(a[i]-i==-1) L=min (l,i), r=Max (y,i);124 } the if(i<=n)Continue;126 if(X<=n && y>0&&!r) Work2 (x, y);127 Else if(L<=n && r>0&&!y) Work2 (l,r); - Else if(Y &&r) Work3 (X,Y,L,R);129 ElseWork4 (); the }131 return 0; the }133 /*134 //135 136 //137 */
View Code
"Analog" CSU 1807 longest ascending subsequence ~ (2016 Hunan province 12th session of the computer Program design contest for university students)