现在的位置: 首页 > 综合 > 正文

hdu 3998 Sequence 求不相交最长上升自序列个数

2013年01月06日 ⁄ 综合 ⁄ 共 2982字 ⁄ 字号 评论关闭

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

//

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=1410;
const int M=50000;
const int inf=(1<<28);
int head[N];
struct Edge
{
    int v,next,w;
    int pw;//原图中u->v的边权
} edge[M];
int cnt,n,s,t;//n从0开始  0->n-1
void addedge(int u,int v,int w)
{
    edge[cnt].v=v;
    edge[cnt].w=w;
    edge[cnt].pw=w;
    edge[cnt].next=head[u];
    head[u]=cnt++;
    edge[cnt].v=u;
    edge[cnt].w=0;
    edge[cnt].pw=w;
    edge[cnt].next=head[v];
    head[v]=cnt++;
}
int sap()
{
    int pre[N],cur[N],dis[N],gap[N];
    int flow=0,aug=inf,u;
    bool flag;
    for(int i=0; i<n; i++)
    {
        cur[i]=head[i];
        gap[i]=dis[i]=0;
    }
    gap[s]=n;
    u=pre[s]=s;
    while(dis[s]<n)
    {
        flag=0;
        for(int &j=cur[u]; j!=-1; j=edge[j].next)
        {
            int v=edge[j].v;
            if(edge[j].w>0&&dis[u]==dis[v]+1)
            {
                flag=1;
                if(edge[j].w<aug) aug=edge[j].w;
                pre[v]=u;
                u=v;
                if(u==t)
                {
                    flow+=aug;
                    while(u!=s)
                    {
                        u=pre[u];
                        edge[cur[u]].w-=aug;
                        edge[cur[u]^1].w+=aug;
                    }
                    aug=inf;
                }
                break;
            }
        }
        if(flag) continue;
        int mindis=n;
        for(int j=head[u]; j!=-1; j=edge[j].next)
        {
            int v=edge[j].v;
            if(edge[j].w>0&&dis[v]<mindis)
            {
                mindis=dis[v];
                cur[u]=j;
            }
        }
        if((--gap[dis[u]])==0)
            break;
        gap[dis[u]=mindis+1]++;
        u=pre[u];
    }
    return flow;
}

//初始化  cnt=0;memset(head,-1,sizeof(head));
int a[800];
int f[800];
int main()
{
    int m;
    while(scanf("%d",&m)==1)
    {
        for(int i=1;i<=m;i++) scanf("%d",&a[i]);
        int maxl=1;
        for(int i=1;i<=m;i++)
        {
            f[i]=1;
            for(int j=1;j<i;j++)
            {
                if(a[j]<a[i]&&f[i]<f[j]+1) f[i]=f[j]+1;
            }
            maxl=max(maxl,f[i]);
        }
        cnt=0;
        memset(head,-1,sizeof(head));
        //每点只能用一次 所以要拆点  容量为1
        n=m*2+2;
        s=0;t=n-1;
        for(int i=1;i<=m;i++)
        {
            addedge(i,i+m,1);
            if(f[i]==maxl)
            {
                addedge(i+m,t,1);
            }
            if(f[i]==1)
            {
                addedge(0,i,1);
            }
            for(int j=1;j<i;j++)
            {
                if(f[i]==f[j]+1) addedge(j+m,i,inf);
            }
        }
        int ans=sap();
        printf("%d\n%d\n",maxl,ans);
    }
    return 0;
}

抱歉!评论已关闭.