hdu 3998 最长上升子序列个数+网络流 (最多不相交合法路径数)

Sequence

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1886    Accepted Submission(s): 687


Problem Description
There is a sequence X (i.e. x[1], x[2], ..., x[n]). 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
 

Source
 

Recommend
lcy   |   We have carefully selected several similar problems for you:   3996  3549  3572  3416  3917 

题目要求找出最长子序列长度,并求出最多有多少个达到最长长度的不相交的子序列。

这题本质上跟hdu 3416问题是一样,就是找不相交(重叠)的合法路径最多有多少条。只不过那题的合法路径是满足最短路径(u,v)d[u]+w==d[v],这题的合法路径则是满足最长上升(u,v) dp[u]+1==dp[v]。做法是一样的,先构造出只包含合法路径的图,把属于合法路径的边(保证每次跑的流量都是属于合法路径)加入到图中,然后跑一遍最大流算法。

这题为了处理方便加入0和n+1两个点,x[0]=-inf,x[n+1]=inf,这样所有的上升子序列肯定是以0为起点,以n+1,为终点。(相当于跟图加入源点和汇点)


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

const int maxn=1e3+10;
const int maxm=maxn*maxn;
const int inf=0x3f3f3f3f;

struct Edge{
    int v,cap,flow,nxt;
};

int first[maxn],tot;
Edge edge[maxm];
int n,a[maxn];
int dp[maxn];

void addedge(int u, int v, int cap)
{
    edge[tot]=Edge{v,cap,0,first[u]};
    first[u]=tot++;
    edge[tot]=Edge{u,0,0,first[v]};
    first[v]=tot++;
}

void init()
{
    tot=0;
    memset(first, -1, sizeof(first));

    for(int i=1; i<=n; i++){
        scanf("%d", a+i);
    }

    memset(dp, 0, sizeof(dp));
    a[0]=-inf,a[n+1]=inf;
    dp[0]=1;
    for(int i=1; i<=n+1; i++){
        for(int j=0; j<i; j++)
        if(a[j]<a[i]){
            dp[i]=max(dp[i],dp[j]+1);
        }
    }

    for(int i=0; i<=n; i++){
        for(int j=i+1; j<=n+1; j++){
            if(a[i]<a[j] && dp[i]+1==dp[j])
                addedge(i, j, 1);
        }
    }
}

int cur[maxn],dep[maxn],gap[maxn],pre[maxn];

int sap(int st, int ed, int N)
{
    memset(dep,0,sizeof(dep));
    memset(gap,0,sizeof(gap));
    memcpy(cur, first, sizeof(first));
    gap[0]=N;
    int u=st, ans=0;
    pre[u]=-1;
    while(dep[st]<N){
        if(u==ed){
            int mini=inf;
            for(int i=pre[u]; i!=-1; i=pre[edge[i^1].v])
                mini=min(mini,edge[i].cap-edge[i].flow);
            for(int i=pre[u]; i!=-1; i=pre[edge[i^1].v]){
                edge[i].flow+=mini;
                edge[i^1].flow-=mini;
            }
            ans+=mini;
            u=st;
            continue;
        }

        bool flag=false;
        for(int i=cur[u]; i!=-1; i=edge[i].nxt){
            int v=edge[i].v;
            if(edge[i].cap-edge[i].flow>0 && dep[v]+1==dep[u]){
                pre[v]=cur[u]=i;
                u=v;
                flag=true;
                break;
            }
        }

        if(flag) continue;

        int mini=N;
        for(int i=first[u]; i!=-1; i=edge[i].nxt){
            int v=edge[i].v;
            if(edge[i].cap-edge[i].flow>0 && dep[v]<mini){
                cur[u]=i;
                mini=dep[v];
            }
        }
        gap[dep[u]]--;
        if(!gap[dep[u]]) return ans;
        dep[u]=mini+1;
        gap[dep[u]]++;
        if(u!=st)
            u=edge[pre[u]^1].v;
    }
    return ans;
}



int main()
{
    while(cin>>n){
        init();
        int ans=sap(0, n+1, n+2);
        cout<<dp[n+1]-2<<endl<<ans<<endl;
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值