HDU 3998 Sequence 最長上升子序列+最大流

來源:互聯網
上載者:User
/*很典型的題了,求最長上升子序列就不贅述了現在要得到不重複利用元素,可以構造幾個這樣長度的序列和同類型的求幾條最短路類似,Maxlen[i]表示到i的最長序列長度Maxlen[i]=1的連源點Maxlen[i]=maxlen的連匯點(maxlen為最長序列的長度)對Maxlen[i]=Maxlen[j]+1(1<=j<i)連一條j到i的邊所有邊邊權為1,得到的最大流就是可以構造的最大個數*/#include <cstdio>#include <iostream>#include <memory.h>#include<queue>#include<set>#include<ctime>#include<algorithm>#include<cmath>#include<vector>#define  LL __int64using namespace std;const int maxn=10009;const LL inf=(1LL)<<35;int aMaxlen[1009];int b[1009];struct edge    {         int v, next;         LL val;     } net[ 500010 ];     int n,m;  int level[maxn], Qu[maxn], out[maxn],next[maxn];    class Dinic {     public:         int end;        Dinic() {             end = 0;             memset( next, -1, sizeof(next) );         }         inline void insert( int x, int y, LL c) {             net[end].v = y, net[end].val = c,            net[end].next = next[x],             next[x] = end ++;             net[end].v = x, net[end].val = 0,            net[end].next = next[y],             next[y] = end ++;         }         bool BFS( int S, int E ) {             memset( level, -1, sizeof(level) );             int low = 0, high = 1;             Qu[0] = S, level[S] = 0;             for( ; low < high; ) {                 int x = Qu[low];                 for( int i = next[x]; i != -1; i = net[i].next ) {                     if( net[i].val == 0 ) continue;                     int y = net[i].v;                     if( level[y] == -1 ) {                         level[y] = level[x] + 1;                         Qu[ high ++] = y;                     }                 }                 low ++;             }             return level[E] != -1;         }                LL MaxFlow( int S, int E ){             LL maxflow = 0;             for( ; BFS(S, E) ; ) {                 memcpy( out, next, sizeof(out) );                 int now = -1;                 for( ;; ) {                     if( now < 0 ) {                         int cur = out[S];                         for(; cur != -1 ; cur = net[cur].next )                              if( net[cur].val && out[net[cur].v] != -1 && level[net[cur].v] == 1 )                                 break;                         if( cur >= 0 ) Qu[ ++now ] = cur, out[S] = net[cur].next;                         else break;                     }                     int u = net[ Qu[now] ].v;                     if( u == E ) {                         LL flow = inf;                         int index = -1;                         for( int i = 0; i <= now; i ++ ) {                             if( flow > net[ Qu[i] ].val )                                 flow = net[ Qu[i] ].val, index = i;                         }                         maxflow += flow;                         for( int i = 0; i <= now; i ++ )                             net[Qu[i]].val -= flow, net[Qu[i]^1].val += flow;                         for( int i = 0; i <= now; i ++ ) {                             if( net[ Qu[i] ].val == 0 ) {                                 now = index - 1;                                 break;                             }                         }                     }                     else{                         int cur = out[u];                         for(; cur != -1; cur = net[cur].next )                              if (net[cur].val && out[net[cur].v] != -1 && level[u] + 1 == level[net[cur].v])                                 break;                         if( cur != -1 )                             Qu[++ now] = cur, out[u] = net[cur].next;                         else out[u] = -1, now --;                     }                 }             }             return maxflow;         }     };     int main(){while(scanf("%d",&n)!=EOF){for(int i=1;i<=n;i++)scanf("%d",&b[i]);aMaxlen[1]=1;for(int i=2;i<=n;i++){int nTmp=0;for(int j=1;j<i;j++)if(b[i]>b[j]){if(aMaxlen[j]>nTmp)nTmp=aMaxlen[j];}aMaxlen[i]=nTmp+1;}int nMax=-1;for(int i=1;i<=n;i++)if(nMax<aMaxlen[i])nMax=aMaxlen[i];Dinic my;int end=1009;for(int i=1;i<=n;i++){if(aMaxlen[i]==nMax)my.insert(i,end,1);if(aMaxlen[i]==1)my.insert(0,i,1);}for(int i=1;i<=n;i++)for(int j=i+1;j<=n;j++){if(aMaxlen[j]==aMaxlen[i]+1)my.insert(i,j,1);}printf("%d\n%I64d\n",nMax,my.MaxFlow(0,end));}return 0;} 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.