http://acm.hdu.edu.cn/showproblem.php?pid=3998

求LIS的长度,并且求有多少组互不相交的LIS

求组数用最大流

建图如下:

if(dp[i]==1)add(S,i,1) ;
if(dp[i]==ans)add(i+n,T,1) ;

if(j>i && dp[j]==dp[i]+1)add(i+n,j,1) ;

如此到汇点1的流量就代表1组LIS的解

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std ;
const int INF=0xfffffff ;
struct node
{
int s,t,cap,nxt ;
}e[] ;
int m,n,cnt,head[],level[],q[] ;
void add(int s,int t,int cap)
{
e[cnt].s=s ;e[cnt].t=t ;e[cnt].cap=cap ;e[cnt].nxt=head[s] ;head[s]=cnt++ ;
e[cnt].s=t ;e[cnt].t=s ;e[cnt].cap= ;e[cnt].nxt=head[t] ;head[t]=cnt++ ;
}
bool build(int s,int t)
{
int front=,rear= ;
memset(level,-,sizeof(level)) ;
q[rear++]=s ;
level[s]= ;
while(front<rear)
{
int u=q[front++] ;
for(int i=head[u] ;i!=- ;i=e[i].nxt)
{
int tt=e[i].t ;
if(level[tt]==- && e[i].cap>)
{
level[tt]=level[u]+ ;
if(tt==t)return true ;
q[rear++]=tt ;
}
}
}
return false ;
}
int find(int s,int t,int flow)
{
if(s==t)return flow ;
int ret=,a ;
for(int i=head[s] ;i!=- ;i=e[i].nxt)
{
int tt=e[i].t ;
if(level[tt]==level[s]+ && e[i].cap>)
{
a=find(tt,t,min(e[i].cap,flow-ret)) ;
e[i].cap-=a ;
e[i^].cap+=a ;
ret+=a ;
if(ret==flow)
return ret ;
}
}
if(!ret)level[s]=- ;
return ret ;
}
int dinic(int s,int t)
{
int flow,ret= ;
while(build(s,t))
while(flow=find(s,t,INF))
ret+=flow ;
return ret ;
}
int a[],dp[] ;
int main()
{
while(~scanf("%d",&n))
{
for(int i= ;i<=n ;i++)
scanf("%d",&a[i]) ;
int ans= ;
for(int i= ;i<=n ;i++)
{
dp[i]= ;
for(int j= ;j<i ;j++)
if(a[j]<a[i] && dp[j]+>dp[i])
dp[i]=dp[j]+ ;
ans=max(ans,dp[i]) ;
}
int S,T ;
S= ;T=*n+ ;
cnt= ;
memset(head,-,sizeof(head)) ;
for(int i= ;i<=n ;i++)
{
add(i,i+n,) ;
if(dp[i]==)add(S,i,) ;
if(dp[i]==ans)add(i+n,T,) ;
for(int j=i+ ;j<=n ;j++)
{
if(dp[j]==dp[i]+)
add(i+n,j,) ;
}
}
printf("%d\n%d\n",ans,dinic(S,T)) ;
}
return ;
}

HDU 3998的更多相关文章

  1. HDU 3998 Sequence(经典问题,最长上升子序列)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3998 解题报告:求一个数列的最长上升子序列,并求像这样长的不相交的子序列最多有多少个. 我用的是最简单 ...

  2. HDU 3998 Sequence (最长上升子序列+最大流)

    参考链接:http://www.cnblogs.com/gentleh/archive/2013/03/30/2989958.html 题意:求一个序列的最长上升子序列,及其个数(注意:两个最长上升子 ...

  3. hdu 3998 (dp+最大流)

    题意:求最长上升子序列的长度和数量. 分析:用dp求出最长上升子序列m,dp数组存的就是该元素为子序列结尾的长度,源点与长度为1的点建边,长度为m的与汇点连边,然后枚举任意两个元素,ai,aj(ai& ...

  4. hdu 3998 Sequence

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

  5. 【转载】图论 500题——主要为hdu/poj/zoj

    转自——http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并 ...

  6. hdu图论题目分类

    =============================以下是最小生成树+并查集====================================== [HDU] 1213 How Many ...

  7. HDU图论题单

    =============================以下是最小生成树+并查集====================================== [HDU] 1213 How Many ...

  8. HDOJ 2111. Saving HDU 贪心 结构体排序

    Saving HDU Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  9. 【HDU 3037】Saving Beans Lucas定理模板

    http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...

随机推荐

  1. Linux——用户管理简单学习笔记(三)

    用户组管理命令: groupadd -g 888 webadmin 创建用户组webadmin,其GID为888 删除用户组: groupdel 组名 修改用户组信息 groupmod groupmo ...

  2. hdu 3706 Second My Problem First 单调队列

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3706 Second My Problem First Time Limit: 12000/4000 M ...

  3. python 获取本地语言和编码的代码

    #! /usr/bin/env python # encoding=utf8 import locale language, encoding = locale.getdefaultlocale() ...

  4. Charles Proxy License 破解

    // Charles Proxy License // 适用于Charles任意版本的注册码,谁还会想要使用破解版呢. // Charles 4.2目前是最新版,可用.   Registered Na ...

  5. django QueryDict对象

    类的原型:class QueryDict[source] 在HttpRequest对象中,GET和POST属性都是一个django.http.QueryDict的实例.也就是说你可以按本文下面提供的方 ...

  6. openstack 问题一览(持续总结中)

    ★名词 Qemu:它也是一种虚拟化技术,主要提供对IO,网络等外设的虚拟化管理.结合KVM(对CPU和内存管理),提供较为完整的虚拟化管理功能. Libvirt:提供了针对各种虚拟机技术的接口,来管理 ...

  7. SpringBoot+Mybatis-Generator自动生成

    原文链接 1.版本 Spring Boot 1.5.10 mybatis-generator-core 1.3.5 mybatis-generator-maven-plugin 1.3.5 2.项目目 ...

  8. <yii 框架学习> yii 框架改为中文提示

    工作需要用到yii框架,但发现yii框架自带的提示都是英文的.上网找资料才发现其实可以自己陪置 . 将项目protected/config/main.php里的app配置加上language=> ...

  9. Mass Change Queries CodeForces - 911G (线段树合并)

    链接 大意: 给定序列, 每次操作将区间[l,r]中的x全改为y, 最后输出序列 权值范围比较小, 对每个权值开一颗线段树, 每次将x合并到y上即可 #include <iostream> ...

  10. OC @property @synthesize和id

    文顶顶   OC语言@property @synthesize和id OC语言@property @synthesize和id 一.@property @synthesize关键字 注意:这两个关键字 ...