hdu3998 Sequence(最大流,LIS)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud
Sequence
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1589 Accepted Submission(s): 587
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).
have n numbers.
3 6 2 5
2
题意:
给出一个序列,问LIS的长度以及长度为LIS长度的不相交的上升序列的个数
分析:
n2 求出LIS,同时维护好信息,然后构图。
若dp[i]=1,则由源点向该点连一条容量为1的边,若dp[i]=dp[j]+1,则由j向i连一条容量为1的边,若dp[i]=LIS的长度,则由i向汇点连一条容量为1的边。
注意要拆点。虽然这题数据比较水,不拆点也能过。
//#####################
//Author:fraud
//Blog: http://www.cnblogs.com/fraud/
//#####################
#include <iostream>
#include <sstream>
#include <ios>
#include <iomanip>
#include <functional>
#include <algorithm>
#include <vector>
#include <string>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <climits>
#include <cctype>
using namespace std;
#define XINF INT_MAX
#define INF 0x3FFFFFFF
#define MP(X,Y) make_pair(X,Y)
#define PB(X) push_back(X)
#define REP(X,N) for(int X=0;X<N;X++)
#define REP2(X,L,R) for(int X=L;X<=R;X++)
#define DEP(X,R,L) for(int X=R;X>=L;X--)
#define CLR(A,X) memset(A,X,sizeof(A))
#define IT iterator
typedef long long ll;
typedef pair<int,int> PII;
typedef vector<PII> VII;
typedef vector<int> VI;
struct edge{
int to,cap,rev;
edge(int _to,int _cap,int _rev)
{
to=_to;
cap=_cap;
rev=_rev;
}
};
const int MAX_V=;
vector<edge>G[MAX_V];
int iter[MAX_V];
int level[MAX_V];
int tot=;
void add_edge(int from,int to,int cap)
{
G[from].PB(edge(to,cap,G[to].size()));
G[to].PB(edge(from,,G[from].size()-));
}
void bfs(int s,int t)
{
CLR(level,-);
queue<int>q;
level[s]=;
q.push(s);
while(!q.empty())
{
int u=q.front();
q.pop();
for(int i=;i<G[u].size();i++)
{
edge &e=G[u][i];
if(e.cap>&&level[e.to]<)
{
level[e.to]=level[u]+;
q.push(e.to);
}
}
}
}
int dfs(int v,int t,int f)
{
if(v==t)return f;
for(int &i=iter[v];i<G[v].size();i++)
{
edge &e=G[v][i];
if(e.cap>&&level[v]<level[e.to])
{
int d=dfs(e.to,t,min(f,e.cap));
if(d>)
{
e.cap-=d;;
G[e.to][e.rev].cap+=d;
return d;
}
}
}
return ;
}
int Dinic(int s,int t)
{
int flow=;
for(;;)
{
bfs(s,t);
if(level[t]<)return flow;
memset(iter,,sizeof(iter));
int f;
while((f=dfs(s,t,INF))>)
{
flow+=f;
}
}
} int a[MAX_V];
int dp[MAX_V];
int main()
{
ios::sync_with_stdio(false);
int n;
while(scanf("%d",&n)!=EOF){
for(int i=;i<n;i++)
scanf("%d",&a[i]);
int ans=;
CLR(dp,);
for(int i=;i<n;i++){
dp[i]=;
for(int j=;j<i;j++){
if(a[j]<a[i]){
dp[i]=max(dp[i],dp[j]+);
}
}
ans=max(ans,dp[i]);
}
int s=*n,t=*n+;
for(int i=;i<t+;i++)G[i].clear();
for(int i=;i<n;i++)add_edge(i,i+n,);
for(int i=;i<n;i++){
if(dp[i]==)add_edge(s,i,);
if(dp[i]==ans)add_edge(i+n,t,);
for(int j=i+;j<n;j++){
if(dp[j]==dp[i]+&&a[i]<a[j]){
add_edge(i+n,j,);
}
}
}
printf("%d\n",ans);
printf("%d\n",Dinic(s,t)); }
return ;
}
代码君
hdu3998 Sequence(最大流,LIS)的更多相关文章
- Codeforces 486E LIS of Sequence(线段树+LIS)
题目链接:Codeforces 486E LIS of Sequence 题目大意:给定一个数组.如今要确定每一个位置上的数属于哪一种类型. 解题思路:先求出每一个位置选的情况下的最长LIS,由于開始 ...
- Python3 常用的几个内置方法
目录 max()/min() filter() 过滤 map() 映射 sorted筛选 reduce()减少 max()/min() 传入一个参数 (可迭代对象), 返回这个可迭代对象中最大的元素 ...
- Codeforces Round #277 (Div. 2) E. LIS of Sequence DP
E. LIS of Sequence Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/486/pr ...
- 【CF486E】LIS of Sequence题解
[CF486E]LIS of Sequence题解 题目链接 题意: 给你一个长度为n的序列a1,a2,...,an,你需要把这n个元素分成三类:1,2,3: 1:所有的最长上升子序列都不包含这个元素 ...
- BZOJ.3532.[SDOI2014]LIS(最小割ISAP 退流)
BZOJ 洛谷 \(LIS\)..经典模型? 令\(f_i\)表示以\(i\)结尾的\(LIS\)长度. 如果\(f_i=1\),连边\((S,i,INF)\):如果\(f_i=\max\limits ...
- Codeforces 486E LIS of Sequence
LIS of Sequence 我们先找出那些肯定不会再LIS里面. 然后我们从前往后扫一次, 当前位置为 i , 看存不存在一个 j 会在lis上并且a[ j ] > a[ i ], 如果满足 ...
- 【BZOJ-3532】Lis 最小割 + 退流
3532: [Sdoi2014]Lis Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 704 Solved: 264[Submit][Status] ...
- [bzoj3532][Sdoi2014]Lis——拆点最小割+字典序+退流
题目大意 给定序列A,序列中的每一项Ai有删除代价Bi和附加属性Ci.请删除若 干项,使得4的最长上升子序列长度减少至少1,且付出的代价之和最小,并输出方案. 如果有多种方案,请输出将删去项的附加属性 ...
- BZOJ3532 [Sdoi2014]Lis 【网络流退流】
题目 给定序列A,序列中的每一项Ai有删除代价Bi和附加属性Ci.请删除若 干项,使得4的最长上升子序列长度减少至少1,且付出的代价之和最小,并输出方案. 如果有多种方案,请输出将删去项的附加属性排序 ...
随机推荐
- ANSI与UINCODE编码
简要说明: ANSI是一种字符代码,为使计算机支持更多语言,通常使用 0x80~0xFF 范围的 2 个字节来表示 1 个字符. Uincode(统一码.万国码.单一码)是计算机科学领域里的一项业界标 ...
- CODEVS 3279 奶牛的健美操
3279 奶牛健美操 USACO 时间限制: 2 s 空间限制: 256000 KB 题目等级 : 钻石 Diamond 题目描述 Description Farmer John为了保持奶牛们的 ...
- Mysql int(11) 和 int(1)
Mysql 可以为整数类型制定宽度,例如:int(11) ,对大多数应用这是没有意义的:它不会限制值的合法范围,它只是规定了Mysql的一些交互工具(例如mysql命令行客户端)用来显示字符个数.对于 ...
- wind7下搭建ftp服务器
一.首先在本地机器上创建一个用户!这些用户是用来登录到FTP的!我的电脑右键->控制面板->管理工具->计算机管理->本地用户和组->用户->“右键”新建用户-&g ...
- JDK PATH 和 CLASSPATH环境变量的作用及其配置
(1)PATH环境变量的作用 在安装JDK程序之后,在安装目录下的bin目录中会提供一些开发Java程序时必备的工具程序. 对于Java的初学者,建议在命令符模式下使用这些工具程序编译运行Java程序 ...
- OC与Swift混编
群里大神发的网址,感觉有用就先收录了,暂时没时间看SWIFT,感觉代码简洁,但是可阅读性不是太高,有些代码让系统去判断类型,同样的,我们看代码的时候也得自己去判断类型,或许看多就习惯了,有时间再说吧, ...
- VC2010的破解方法(针对旗舰版)
VS2010 正式版破解方法详解 全球开发者最为瞩目的Visual Studio 2010开发工具在4月12日正式发布,现为大家制作一个简单的破解教程有两种方法,操作不一样,原都一样(针对旗舰版,其他 ...
- WPF 如何缓解大量控件加载缓慢的问题
最近有一个项目需要加载大量的控件,导致系统出现卡顿问题,经过几天的努力,终于搞定了,写一下备忘. 解决方案是首次加载时只显示可见区域控件,之后使用辅助线程进行分批加载,将分批加载的线程优先级别设置为空 ...
- cygwin安装与使用
cygwin安装很简单,下载运行setup.exe程序,一步一步就可以了. 具体安装细节参考:http://www.33lc.com/article/7276.html 安装完成后有如下问题: 在cm ...
- ssh添加公钥
ssh-keygen生成公钥存储在文件: ~/.ssh/id_rsa.pub 如果ssh-add -l命令后没有一串长的字符串, 把私钥密钥对的ID(fingerPrint)加入ssh的认证代理ssh ...