"Turn Hzwer" The first question is LIS, dynamic programming solution, second and third ask with network maximum flow solution. First, the dynamic programming of the f[i], indicating the length of the longest ascending sequence starting with the first bit, the longest ascending sequence length k is obtained. 1, the sequence of each I split into two points <i.a> and <i.b>, from <i.a> to <i.b> to connect a capacity of 1 with a forward edge. 2, the establishment of additional sources s and sinks T, if the sequence I-bit has f[i]=k, from S to <i.a> to connect a capacity of 1 with a forward edge. 3, if f[i]=1, from <i.b> to T connect a capacity of 1 with a forward edge. 4, if J>i and A[i] < A[J] and F[j]+1=f[i], from <i.b> to <j.a> to connect a capacity 1 of the forward edge. Finding the maximum flow of the network is the result of the second question. The capacity of the Edge (<1.a>,<1.b>) (<N.a>,<N.b>) (s,<1.a>) (<n.b>,t) four edges is modified to infinity, and then the maximum network flow is calculated. Is the result of the third question. The problem of network flow is solved by dynamic regulation.
#include <iostream>#include<algorithm>#include<cstdio>#include<cstdlib>#include<cstring>#include<cmath>#include<ctime>#include<queue>#include<stack>#include<vector>using namespacestd;#defineDebugTemplate<Const int_n,Const int_m>structedge{structEdge_base {intTo,next,w; }E[_M];intCnt,p[_n]; Edge () {clear ();} voidInsertConst intXConst intYConst intz) {e[++cnt].to=y; E[CNT].NEXT=P[X]; E[cnt].w=z; p[x]=cnt;return ; } intStartConst intx) {returnp[x];} voidClear () {cnt=1, Memset (P,0,sizeof(p)); } edge_base&operator[](Const intx) {returne[x];}; Edge<11000,1100000>e;intans=0, TAns;intn,a[5100],cur[11000],level[11000],sss,ttt;intf[11000];BOOLBfs (Const intS) { inti,t; Queue<int>Q; Memset (Level,0,sizeof(level)); Level[s]=1; Q.push (S); while(!Q.empty ()) {T=Q.front (), Q.pop (); for(I=e.start (t); i;i=E[i].next) { if(!level[e[i].to] &&E[I].W) {Level[e[i].to]=level[t]+1; Q.push (e[i].to); } } } returnlevel[ttt];}intDfs (Const intSConst intBK) { if(S==TTT)returnBK; intrest=BK; for(int&i=cur[s];i;i=E[i].next) { if(level[e[i].to]==level[s]+1&&E[I].W) { intflow=Dfs (E[i].to,min (REST,E[I].W)); E[I].W-=flow; E[i^1].w+=flow; if((Rest-=flow) <=0) Break; } } if(REST==BK) level[s]=0; returnbk-rest;}intDinic () {intflow=0; while(Bfs (SSS)) {memcpy (CUR,E.P,sizeof(cur)); Flow+=dfs (SSS,0x3f3f3f3f); } returnflow;}voidCalc1 () {inti,j; for(i=1; i<=n;++i) {if(f[i]==1) E.insert (Sss,i,1), E.insert (I,sss,0); if(F[i]==ans) E.insert (I+N,TTT,1), E.insert (Ttt,i+n,0); E.insert (I,i+n,1); E.insert (i+n,i,0); } for(i=1; i<=n;++i) { for(j=i+1; j<=n;++j) {if(A[j]>=a[i] && f[j]==f[i]+1) E.insert (i+n,j,1), E.insert (J,i+n,0); }} printf ("%d\n", tans=dinic ());}voidCalc2 () {inti,j; E.clear (); for(i=1; i<=n;++i) {intv=1; if(i==1|| I==n) v=0x3f3f3f3f; if(f[i]==1) E.insert (sss,i,v), E.insert (I,sss,0); if(F[i]==ans) E.insert (i+n,ttt,v), E.insert (Ttt,i+n,0); E.insert (I,i+n,v); E.insert (i+n,i,0); } for(i=1; i<=n;++i) { for(j=i+1; j<=n;++j) {if(A[j]>=a[i] && f[j]==f[i]+1) E.insert (i+n,j,1), E.insert (J,i+n,0); } } inttemp=Dinic (); if(temp>=0x3f3f3f3f) temp=TAns; printf ("%d\n", temp); return ;}intMain () {Freopen ("alis.in","R", stdin); Freopen ("Alis.out","W", stdout); inti,j; scanf ("%d",&N); for(i=1; i<=n;++i) scanf ("%d",&A[i]); for(i=1; i<=n;++i) {F[i]=1; for(j=1; j<i;++j)if(A[j]<=a[i]) F[i]=max (f[i],f[j]+1); Ans=Max (ans,f[i]); } printf ("%d\n", Ans); SSS=n<<1|1, ttt=sss+1; Calc1 (); CALC2 (); return 0;}
[cogs731] [Network Stream 24] maximum increment subsequence [network flow, maximum flow]