HDU 3998 Sequence (最长上升子序列+最大流)
参考链接:
http://www.cnblogs.com/gentleh/archive/2013/03/30/2989958.html
题意:求一个序列的最长上升子序列,及其个数(注意:两个最长上升子序列不能共有同一个数,才能算两个)
思路:用dp+最大流来求,首先用两次循环dp求出最长上升子序列的长度ans,然后就是建图了,可以选择源点为0,
由于数列中的每一个数只能使用一次,构图的时候需要拆点。若有n个数,则拆成2 * n个点,构造源点s=0和汇点t=2*n+1,
将每个点(i)都和自己拆出来的点(i+n)连边,将源点和dp[i]=1的点连边,将dp[i]=ans的点与汇点连边,
最后若dp[j] = dp[i] + 1,则将i + n和 j连边。所有边的流量都是1,这样便可以限制每个点只使用一次。
其实连边的顺序就是最长序列为1,2,3,...,ans。可以理解为从最长序列为1(该数本身)一直流到数列中的最长上升序列。
这里要注意的是,由于数据弱,有不用拆点也能A的。但是为了保证每个点都是用一次,一定要拆点。
如果不拆点的话,可以见下图,则中间那个点可能会被用两次,具体不仔细说了。
我一开始按照dinic模板打了一遍,结果TLE。。。后来经别人指点,加了dinic中的两个判断条件,见(1)和(2),瞬间A了。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include <queue>
#define INF 0x3f3f3f3f using namespace std;
const int maxn=;
int n,m;
int a[maxn]; //序列
int pri[maxn*];
int head[maxn*]; //因为要拆点
int s,t; //s:源点 t:汇点
int dp[maxn];
int tot=;
int ans,num;//最长上升子序列的长度,以及不同的个数。 struct Edge{
int from,to;
int next;
int c,f;
}edge[maxn*]; void add(int x,int y,int val){
edge[tot].next=head[x];
edge[tot].from=x;
edge[tot].to=y;
edge[tot].c=val; //注意这里是c为val,反向边c为0
//edge[tot].f=0;
head[x]=tot++; edge[tot].next=head[y];
edge[tot].from=y;
edge[tot].to=x;
edge[tot].c=;
//edge[tot].f=0;
head[y]=tot++;
}
bool BFS(){
queue<int>q;
memset(pri,-,sizeof(pri));
pri[]=;//源点
q.push();
while(!q.empty()){
int tmp=q.front();
q.pop();
for(int k=head[tmp];k!=-;k=edge[k].next){
int v=edge[k].to;
if(pri[v]==- && edge[k].c>){
pri[v]=pri[tmp]+;
if(v==t){
return true;
}
q.push(v);
}
}
}
return false;
} int dinic(int p,int flow){
if(p==t){
return flow;
}
int f=flow;
for(int k=head[p];k!=-;k=edge[k].next){
int v=edge[k].to;
if(pri[v]==pri[p]+ && edge[k].c>){
int a=edge[k].c; //a为该边可以增加的流量
int ff=dinic(v,min(a,flow)); //ff为路径中所有a的最小值,即为该条路中可以增加的流量
//edge[k].f+=ff;
//edge[k^1].f-=ff; //因为双向边是同时建的,编号必定一奇一偶成对的,如0、1;2、3;4、5;
//这样用k异或1即可求得对应的反向边
edge[k].c-=ff;
edge[k^].c+=ff;
flow-=ff;
if(flow<=)
break; //(1)
}
}
if(f-flow<=)
pri[p]=-; //(2)
//如果从p点流出去的流量<=0,那么设置pri[p]的值为-1,之后在dinic中就不考虑到p点的情况了。
return f-flow;
} int main()
{
while(scanf("%d",&n)!=EOF){
for(int i=;i<=n;i++){
dp[i]=;
scanf("%d",&a[i]);
}
for(int i=;i<=n;i++){
for(int j=;j<i;j++){
if(a[i]>a[j] && dp[j]+>dp[i])
dp[i]=dp[j]+;
}
}
m=*n+;
ans=;
for(int i=;i<=n;i++){
ans=max(dp[i],ans);
}
memset(head,-,sizeof(head));
tot=; s=;
t=*n+;
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 i=;i<=n;i++){
//下面j只要从i+1开始即可,因为add加边是加双向边的
for(int j=i+;j<=n;j++){
if(dp[j]==dp[i]+){
add(i+n,j,);
}
}
}
num=;
while(BFS()){
num+=dinic(s,INF);
}
printf("%d\n%d\n",ans,num); }
return ;
}
HDU 3998 Sequence (最长上升子序列+最大流)的更多相关文章
- HDU 3998 Sequence(经典问题,最长上升子序列)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3998 解题报告:求一个数列的最长上升子序列,并求像这样长的不相交的子序列最多有多少个. 我用的是最简单 ...
- hdu 5748(求解最长上升子序列的两种O(nlogn)姿势)
Bellovin Time Limit: / MS (Java/Others) Memory Limit: / K (Java/Others) Total Submission(s): Accepte ...
- HDU 4681 String 最长公共子序列
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=4681 题意: 给你a,b,c三个串,构造一个d串使得d是a,b的子序列,并且c是d的连续子串.求d最大 ...
- HDU 1159.Common Subsequence-最长公共子序列(LCS)
Common Subsequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- 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], ...
- hdu 1025 dp 最长上升子序列
//Accepted 4372 KB 140 ms //dp 最长上升子序列 nlogn #include <cstdio> #include <cstring> #inclu ...
- hdu 5489(LIS最长上升子序列)
题意:一个含有n个元素的数组,删去k个连续数后,最长上升子序列 /*思路参考GoZy 思路: 4 2 3 [5 7 8] 9 11 ,括号表示要删掉的数, 所以 最长上升子序列 = ...
- hdu 5532(最长上升子序列)
Input The first line contains an integer T indicating the total number of test cases. Each test case ...
- Cogs 731. [网络流24题] 最长递增子序列(最大流)
[网络流24题] 最长递增子序列 ★★★☆ 输入文件:alis.in 输出文件:alis.out 简单对比 时间限制:1 s 内存限制:128 MB «问题描述: 给定正整数序列x1,-, xn. ( ...
随机推荐
- 【Cocoa】 Initializing View Instances Created in Interface Builder
Initializing View Instances Created in Interface Builder View instances that are created in Interfac ...
- C 基于UDP实现一个简易的聊天室
引言 本文是围绕Linux udp api 构建一个简易的多人聊天室.重点看思路,帮助我们加深 对udp开发中一些api了解.相对而言udp socket开发相比tcp socket开发注意的细节要少 ...
- iOS之push present 动画
直接源码: - (void) transitionWithType:(NSString *) type WithSubtype:(NSString *) subtype ForView : (UIVi ...
- 第2章 HelloWorld
----------------------------------------- 2-2 在设置应用图标的时候,一定要注意添加的图片一定是.ico 的要求格式(32*32像素). 可以在网上下载 ...
- 《verilog数字系统设计教程》书评
这本书的确是一本很经典的关于verilog语法和一些基本概念的书籍,后面的例子也很好,但是对于初学者来说,我们需要掌握的是语法和一些基本的概念. 刚一开始这本书的中文语法有点不是很通顺,但是越是往后, ...
- 25.usb固件深入
dscr51里放的是USB描述符表,EZ-USB在重枚举阶段会读取或设置相应的描述符: db DSCR_DEVICE_LEN ;; Descriptor length db ...
- Learning Java language Fundamentals
Chapter 2 Learning Java language fundamentals exercises: 1.What is Unicode? Unicode is a computing ...
- bzoj 3232 01分数规划+最大权封闭子图判定
我们的目标是使v/c最小化,所以构造函数g(x)=v-x*c,那么 二分一个X,判断当时的v-x*c的值是多少,然后根据g(x)函数的 单调递减性来二分,判断,直到g(x)=0的时候当前的X就是答案. ...
- poj 1330 Nearest Common Ancestors LCA
题目链接:http://poj.org/problem?id=1330 A rooted tree is a well-known data structure in computer science ...
- ios 流媒体 资料
(1)“Real-Time Streaming Protocol (RTSP)”比较官方的资料(链接). (2)“ffmpeg”在百度百科的简介(链接). (3)“ffmpeg”的官网:http:// ...