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

本文介绍了一种寻找序列中最长递增子序列的方法,并进一步探讨了如何确定该长度子序列的数量。通过实例说明了算法的具体实现过程,包括如何构建图模型并使用改进的最短路径算法进行求解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值