hdu 3998(DP+最大流)

Sequence

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


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
 
分析:此题第一解相信每个人都会,直接求最长递增子序列,这回要记录每个数的长度,记下第一个解ans,然后就是第二步了,题目要求取多次的最长序列,而且不能重复取,(因为做过这题的类似题直接想到最大流,囧。。。赚到了),增加源和汇,把所有数看成两个点,这两个点直接连上容量为1 的边,然后就是源与标记长度为1的点的头,标记长度为ans的点的尾与汇,还有所有满足标记长度差一的点头和尾都连上容量为1的边,然后最大流,即第二个解了。。。
代码:
#include<cstdio>
using namespace std;
const int mm=2222222;
const int mn=11111;
const int oo=1000000000;
int node,src,dest,edge;
int ver[mm],flow[mm],next[mm];
int head[mn],work[mn],dis[mn],q[mn],a[mn],f[mn];
inline int min(int a,int b)
{
    return a<b?a:b;
}
inline void prepare(int _node,int _src,int _dest)
{
    node=_node,src=_src,dest=_dest;
    for(int i=0;i<node;++i)head[i]=-1;
    edge=0;
}
inline void addedge(int u,int v,int c)
{
    ver[edge]=v,flow[edge]=c,next[edge]=head[u],head[u]=edge++;
    ver[edge]=u,flow[edge]=0,next[edge]=head[v],head[v]=edge++;
}
bool Dinic_bfs()
{
    int i,u,v,l,r=0;
    for(i=0;i<node;++i)dis[i]=-1;
    dis[q[r++]=src]=0;
    for(l=0;l<r;++l)
        for(i=head[u=q[l]];i>=0;i=next[i])
            if(flow[i]&&dis[v=ver[i]]<0)
            {
                dis[q[r++]=v]=dis[u]+1;
                if(v==dest)return 1;
            }
    return 0;
}
int Dinic_dfs(int u,int exp)
{
    if(u==dest)return exp;
    for(int &i=work[u],v,tmp;i>=0;i=next[i])
        if(flow[i]&&dis[v=ver[i]]==dis[u]+1&&(tmp=Dinic_dfs(v,min(exp,flow[i])))>0)
        {
            flow[i]-=tmp;
            flow[i^1]+=tmp;
            return tmp;
        }
    return 0;
}
int Dinic_flow()
{
    int i,ret=0,delta;
    while(Dinic_bfs())
    {
        for(i=0;i<node;++i)work[i]=head[i];
        while(delta=Dinic_dfs(src,oo))ret+=delta;
    }
    return ret;
}
int main()
{
    int i,j,n,ans;
    while(scanf("%d",&n)!=-1)
    {
        for(i=1;i<=n;++i)scanf("%d",&a[i]),f[i]=1;
        for(i=2;i<=n;++i)
            for(j=1;j<i;++j)
                if(a[j]<a[i]&&f[j]>=f[i])f[i]=f[j]+1;
        for(i=ans=1;i<=n;++i)
            if(f[i]>ans)ans=f[i];
        prepare(n+n+2,0,n+n+1);
        for(i=1;i<=n;++i)
        {
            addedge(i,i+n,1);
            if(f[i]==1)addedge(src,i,1);
            if(f[i]==ans)addedge(i+n,dest,1);
            for(j=i+1;j<=n;++j)
                if(f[j]==f[i]+1)addedge(i+n,j,1);
        }
        printf("%d\n",ans);
        printf("%d\n",Dinic_flow());
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值