24 longest network flow problem does not drop subsequence problem

Topic Portal

The construction of Figure ah, very clever, I really did not see it (I was too betel a)

First you have to run again does not fall longest subsequence \ (O (n ^ 2) \) solution, the pretreatment to \ (I \) the length of the end of the maximum length decrease subsequence \ (dp [i] \ ) , then find the maximum \ (dp [i] \) , denoted \ (cnt \) , output \ (cnt \) , the first question is over.

From a source point to a length \ (1 \) point connected to a capacity \ (1 \) side, the length \ (CNT \) points connected to a sink capacity \ (1 \) side. Because each point can only be used once, so we have to put a point split into two, between them even a capacity of \ (1 \) side. After \ (n ^ 2 \) enumerate every two points, two points if \ (i, j \) satisfies \ (j {<} i \ ) and \ (dp [i] = dp [j] +1 \) , then \ (i, j \) between the capacity of even a \ (1 \) side. After creating the largest flow diagram running side, is the answer to the second question.

Third question will be easier, the source point to \ (1 \) to the side of the capacity even \ (\ RM INF} {\) , \ (1 \) and \ (1 \) point after removed \ (1 '\) capacity also changed between \ (\ RM INF} {\) . Similarly, \ (n-\) and \ (n '\) edge between, (\ \ n-') edges between the sink and (if present) have the capacity to \ (\ rm {INF} \)

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#define INF 2147483647
using namespace std;
struct zzz{
    int t,len,nex;
}e[20010<<1]; int head[10010],tot=1;
void add(int x,int y,int z){
    e[++tot].t=y;
    e[tot].len=z;
    e[tot].nex=head[x];
    head[x]=tot;
}
int dis[10010],s,t;
bool bfs(){
    memset(dis,0,sizeof(dis));
    queue <int> q; q.push(s);
    dis[s]=1;
    while(!q.empty()){
        int k=q.front(); q.pop();
        for(int i=head[k];i;i=e[i].nex){
            int to=e[i].t;
            if(dis[to]||!e[i].len) continue;
            dis[to]=dis[k]+1;
            if(to==t) return 1;
            q.push(to);
        }
    }
    return dis[t];
}
int dfs(int now,int flow){
    if(now==t||!flow) return flow;
    int rest=0,fl;
    for(int i=head[now];i;i=e[i].nex){
        int to=e[i].t;
        if(dis[to]==dis[now]+1&&(fl=dfs(to,min(e[i].len,flow)))){
            e[i].len-=fl, e[i^1].len+=fl, rest+=fl;
            if(rest==flow) return rest;
        }
    }
    if(rest<flow) dis[now]=0;
    return rest;
}
int ans;
int dinic(){
    if(bfs()) ans+=dfs(s,INF);
    return ans;
}
int read(){
    int k=0; char c=getchar();
    for(;c<'0'||c>'9';) c=getchar();
    for(;c>='0'&&c<='9';c=getchar())
      k=k*10+c-48;
    return k;
}
int a[510],dp[510],cnt;
int main(){
    int n=read(); t=(n<<1)+1;
    for(int i=1;i<=n;i++) a[i]=read(),dp[i]=1;
    for(int i=1;i<=n;i++)
      for(int j=i+1;j<=n;j++)
        if(a[j]>=a[i]) dp[j]=max(dp[j],dp[i]+1);
    for(int i=1;i<=n;i++) cnt=max(cnt,dp[i]);
    printf("%d\n",cnt);
    if(cnt==1){
        printf("%d\n%d",n,n);
        return 0;
    }
    for(int i=1;i<=n;i++){
        add(i,i+n,1); add(i+n,i,0);
        if(dp[i]==cnt) add(i+n,t,1),add(t,i+n,0);
        if(dp[i]==1) add(s,i,1),add(i,s,0);
    }
    for(int i=1;i<=n;i++)
      for(int j=1;j<i;j++){
          if(a[j]<=a[i]&&dp[j]+1==dp[i])
              add(j+n,i,1),add(i,n+j,0);
      }
    printf("%d\n",dinic());
    add(1,1+n,INF),add(n+1,1,0);
    add(n,n<<1,INF),add(n<<1,n,0);
    if(dp[1]==1) add(s,1,INF), add(1,s,0);
    if(dp[n]==cnt) add(n<<1,t,INF), add(t,n<<1,0);
    printf("%d\n",dinic());
    return 0;
}

Guess you like

Origin www.cnblogs.com/morslin/p/11854756.html