您的位置:首页 > 理论基础 > 计算机网络

HDU_3998_Sequence(最长上升子序列 + 网络流)

2015-09-18 23:14 537 查看

Sequence

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 1919 Accepted Submission(s): 697


Problem Description
There is a sequence X (i.e. x[1], x[2], ..., x
). We define increasing subsequence of X

as x[i1], x[i2],...,x[ik], which satisfies follow conditions:

1) x[i1] < x[i2],...,<x[ik];

2) 1<=i1 < i2,...,<ik<=n

As an excellent program designer, you must know how to find the maximum length of the

increasing sequense, which is defined as s. Now, the next question is how many increasing

subsequence with s-length can you find out from the sequence X.

For example, in one case, if s = 3, and you can find out 2 such subsequence A and B from X.

1) A = a1, a2, a3. B = b1, b2, b3.

2) Each ai or bj(i,j = 1,2,3) can only be chose once at most.

Now, the question is:

1) Find the maximum length of increasing subsequence of X(i.e. s).

2) Find the number of increasing subsequence with s-length under conditions described (i.e. num).


Input
The input file have many cases. Each case will give a integer number n.The next line will

have n numbers.


Output
The output have two line. The first line is s and second line is num.


Sample Input
4
3 6 2 5




Sample Output
2
2



题意:求最长上升子序列以及最长上升子序列的个数(每个数只能取一次)。

分析:dp + 最大流。首先每个数只能取一次,那么就要对每个点( i )进行拆点,连边 i -> i + N,容量为1;加入超级源点s,对于dp[i] == 1的点,s指向 i 连一条边 s -> i,容量为无穷大;在求最长上升子序列过程中,使得[1,i-1]当中dp[j] + 1 == dp[i]的点 j 向 i 连一条边 j + N -> i,容量为1;加入超级汇点,使得所有dp[i] == LIS的点 i 向 t 连边 i + N -> t,容量为无穷大。然后跑一边最大流即可。

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3998

代码清单:

#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<cctype>
#include<string>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
using namespace std;

#define end() return 0

typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;

const int maxN = 1000 + 5;
const int maxn = 10000 + 5;
const int maxv = 100000 + 5;
const int INF = 0x7f7f7f7f;

struct Edge{
    int from,to,cap,flow;
    Edge(int u,int v,int c,int f):from(u),to(v),cap(c),flow(f){}
};

struct dinic{
    int n,m,s,t; //结点数,边数(包括反向弧),源点,汇点
    vector<Edge>edge;//边表。edge[e]和edge[e^1]互为反向弧
    vector<int>G[maxn];//邻接表。G[i][j]表示结点i的第j条边在e数组的序号
    bool vis[maxn]; //bfs用
    int d[maxn]; //从起点到i的距离
    int cur[maxn]; //当前弧下标

    void init(int n,int s,int t){
        this -> n = n;
        this -> s = s;
        this -> t = t;
        for(int i=0;i<=n;i++) G[i].clear();
        edge.clear();
    }

    void addEdge(int from,int to,int cap){
        edge.push_back(Edge(from,to,cap,0));
        edge.push_back(Edge(to,from,0,0));
        m=edge.size();
        G[from].push_back(m-2);
        G[to].push_back(m-1);
    }

    bool bfs(){
        memset(vis,false,sizeof(vis));
        queue<int>q;
        q.push(s);
        d[s]=0;
        vis[s]=true;
        while(!q.empty()){
            int x=q.front();q.pop();
            for(int i=0;i<G[x].size();i++){
                Edge& e=edge[G[x][i]];
                if(!vis[e.to]&&e.cap>e.flow){ //只考虑残量网络中的弧
                    vis[e.to]=true;
                    d[e.to]=d[x]+1;
                    q.push(e.to);
                }
            }
        }
        return vis[t];
    }

    int dfs(int x,int a){
        if(x==t||a==0) return a;
        int flow=0,f;
        for(int& i=cur[x];i<G[x].size();i++){ // & -> 从上次考虑的弧
            Edge& e=edge[G[x][i]];
            if(d[x]+1==d[e.to]&&(f=dfs(e.to,min(a,e.cap-e.flow)))>0){
                e.flow+=f;
                edge[G[x][i]^1].flow-=f;
                flow+=f;
                a-=f;
                if(a==0) break;
            }
        }
        return flow;
    }

    int maxflow(){
        int flow=0;
        while(bfs()){
            memset(cur,0,sizeof(cur));
            flow+=dfs(s,INF);
        }
        return flow;
    }
};

int N;
int a[maxN];
int dp[maxN];
int tail,maxa;
dinic dc;

void input(){
    for(int i=1;i<=N;i++)
        scanf("%d",&a[i]);
}

void solve(){

    tail=2*N+1;
    dc.init(tail+1,0,tail);
    dc.addEdge(0,1,INF);
    for(int i=1;i<=N;i++){
        dc.addEdge(i,i+N,1);
    }

    dp[1]=1; maxa=-INF;

    for(int i=2;i<=N;i++){
        dp[i]=1;
        for(int j=1;j<i;j++){
            if(a[i]>a[j]&&dp[j]+1>dp[i]){
                dp[i]=dp[j]+1;
            }
        }

        maxa=max(maxa,dp[i]);

        if(dp[i]==1){
            dc.addEdge(0,i,INF);
        }

        else{
            for(int j=1;j<i;j++){
                if(dp[j]+1==dp[i]){
                    dc.addEdge(j+N,i,1);
                }
            }
        }
    }

    for(int i=1;i<=N;i++){
        if(dp[i]==maxa){
            dc.addEdge(i+N,tail,INF);
        }
    }

    printf("%d\n%d\n",maxa,dc.maxflow());
}

int main(){
    while(scanf("%d",&N)!=EOF){
        input();
        solve();
    }end();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: