洛谷P2766 最长不下降子序列问题(最大流)
第一问直接$dp$解决,求出$len$
然后用$f[i]$表示以$i$为结尾的最长不下降子序列长度,把每一个点拆成$A_i,B_i$两个点,然后从$A_i$向$B_i$连容量为$1$的边
然后考虑$f[i]$,如果$f[i]==1$,则从$s$向$A_i$连边,如果$f[i]==len$,那么从$B_i$向$t$连边
然后将每一个$j<i,f[j]+1==f[i],a[j]\leq a[i]$的$j$向$i$连边
以上容量全为$1$
建完图之后跑一个最大流
这样可以保证分层图里选出来的不下降子序列长度必为$len$
然后第三问的话,就把关于$1$和$n$的容量限制给取消掉就好了,就是$s$向$A_1$连$inf$,$A_1$向$B_1$连$inf$,$A_n$向$B_n$连$inf$,$B_n$向$t$连$inf$(如果$f[n]!=len$就不用连了)
然后再跑一次最大流就是第三问的答案
//minamoto
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#define inf 0x3f3f3f3f
using namespace std;
#define getc() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
char buf[<<],*p1=buf,*p2=buf;
template<class T>inline bool cmax(T&a,const T&b){return a<b?a=b,:;}
inline int read(){
#define num ch-'0'
char ch;bool flag=;int res;
while(!isdigit(ch=getc()))
(ch=='-')&&(flag=true);
for(res=num;isdigit(ch=getc());res=res*+num);
(flag)&&(res=-res);
#undef num
return res;
}
const int N=,M=;
int ver[M],Next[M],head[N],edge[M],cur[N],dep[N],tot=,a[N],dp[N];
int n,m,s,t,ans,len=;
queue<int> q;
inline void add(int u,int v,int e){
ver[++tot]=v,Next[tot]=head[u],head[u]=tot,edge[tot]=e;
ver[++tot]=u,Next[tot]=head[v],head[v]=tot,edge[tot]=;
}
bool bfs(){
memset(dep,-,sizeof(dep));
while(!q.empty()) q.pop();
for(int i=;i<=*n+;++i) cur[i]=head[i];
q.push(s),dep[s]=;
while(!q.empty()){
int u=q.front();q.pop();
for(int i=head[u];i;i=Next[i]){
int v=ver[i];
if(dep[v]<&&edge[i]){
dep[v]=dep[u]+,q.push(v);
if(v==t) return true;
}
}
}
return false;
}
int dfs(int u,int limit){
if(!limit||u==t) return limit;
int flow=,f;
for(int i=head[u];i;i=Next[i]){
int v=ver[i];
if(dep[v]==dep[u]+&&(f=dfs(v,min(limit,edge[i])))){
flow+=f,limit-=f;
edge[i]-=f,edge[i^]+=f;
if(!limit) break;
}
}
return flow;
}
void dinic(){
while(bfs()) ans+=dfs(s,inf);
}
int main(){
n=read();
for(int i=;i<=n;++i) a[i]=read(),dp[i]=;
for(int i=;i<=n;++i){
for(int j=;j<i;++j)
if(a[j]<=a[i]) cmax(dp[i],dp[j]+);
cmax(len,dp[i]);
}
printf("%d\n",len);
s=,t=*n+;
for(int i=;i<=n;++i){
if(dp[i]==) add(s,i,);
if(dp[i]==len) add(i+n,t,);
add(i,i+n,);
}
for(int i=;i<=n;++i)
for(int j=;j<i;++j)
if(a[j]<=a[i]&&dp[j]==dp[i]-) add(j+n,i,);
dinic();printf("%d\n",ans);
add(,n+,inf),add(s,,inf);
if(dp[n]==len) add(n,n<<,inf),add(n<<,t,inf);
dinic();printf("%d\n",ans);
return ;
}
洛谷P2766 最长不下降子序列问题(最大流)的更多相关文章
- 洛谷 P2766 最长不下降子序列问【dp+最大流】
死于开小数组的WA?! 第一问n方dp瞎搞一下就成,f[i]记录以i结尾的最长不下降子序列.记答案为mx 第二问网络流,拆点限制流量,s向所有f[i]为1的点建(s,i,1),所有f[i]为mx(i+ ...
- 洛谷 [P2766] 最长不下降子序列问题
啊啊啊,再把MAXN和MAXM搞反我就退役 层次图求不相交路径数 第一问简单DP 第二问想办法把每一个不上升子序列转化成DAG上的一条路径,就转换成了求不相交路径数 因为每一个数只能用一次,所以要拆点 ...
- 洛谷P2766 最长不下降子序列问题 网络流_DP
Code: #include<cstdio> #include<iostream> #include<vector> #include<algorithm&g ...
- 洛谷.T22136.最长不下降子序列(01归并排序 分治)
题目链接 \(Description\) 给定一个长为n的序列,每次可以反转 \([l,r]\) 区间,代价为 \(r-l+1\).要求在\(4*10^6\)代价内使其LIS长度最长,并输出需要操作的 ...
- 【24题】P2766最长不下降子序列问题
网络流二十四题 网络流是个好东西,希望我也会. 网络流?\(orz\ zsy!!!!!\) P2766 最长不下降子序列问题 考虑我们是如何\(dp\)这个\(LIS\)的. 我们是倒着推,设置\(d ...
- [**P2766** 最长不下降子序列问题](https://www.luogu.org/problemnew/show/P2766)
P2766 最长不下降子序列问题 考虑我们是如何\(dp\)这个\(LIS\)的. 我们是倒着推,设置\(dp(i)\)代表以\(i\)为起点的\(LIS\)是多少.转移太显然了 \[ dp(i)=m ...
- P2766 最长不下降子序列问题 网络流重温
P2766 最长不下降子序列问题 这个题目还是比较简单的,第一问就是LIS 第二问和第三问都是网络流. 第二问要怎么用网络流写呢,首先,每一个只能用一次,所以要拆点. 其次,我们求的是长度为s的不下降 ...
- 洛谷P2766 最长递增子序列问题
https://www.luogu.org/problemnew/show/P2766 注:题目描述有误,本题求的是最长不下降子序列 方案无限多时输出 n 网络流求方案数,长见识了 第一问: DP 同 ...
- P2766 最长不下降子序列问题 网络流
link:https://www.luogu.org/problemnew/show/P2766 题意 给定正整数序列x1,...,xn . (1)计算其最长不下降子序列的长度s. (2)计算从给定的 ...
随机推荐
- Select/Poll/Epoll异步IO
IO多路复用 同步io和异步io,阻塞io和非阻塞io分别是什么,有什么样的区别? io模式 对于一次io 访问(以read为例),数据会先拷贝到操作系统内核的缓冲区,然后才会从操作系统内核的缓冲区拷 ...
- leetcode804
int uniqueMorseRepresentations(vector<string>& words) { map<char, string> st; st.ins ...
- Lecture Sleep(前缀和)
Your friend Mishka and you attend a calculus lecture. Lecture lasts n minutes. Lecturer tells ai the ...
- 并发之AtomicIntegerArray
5 并发之AtomicIntegerArray 该类是Java对Integer数组支持的原子性操作:在认识这个类之前我们先来看一个方法,这个方法是Integer类中的: public sta ...
- 代码查看import的类是出自哪个jar包的方法(转)
import java.security.ProtectionDomain; import java.security.CodeSource; public static void main(Stri ...
- Memcached 原理
memcached 是以一个守护进程的方式运行于一个服务器和多个服务器之间的,等待接受客户端的连接操作,客户端可以有各种语言编写.(例如PHP). php 在客户端与服务器建立连接以后,接下来的事情 ...
- 图解KMP算法
- The 'Microsoft Jet OLEDB 4.0 Provider' is not registered on the local machine
在一台Win7 64位的操纵系统上部署的C# Web系统,操作Excel,批量导入数据,报错,提示错误信息: The ‘Microsoft Jet OLEDB 4.0 Provider' is not ...
- 面试题:四种Java线程池用法解析 !=!=未看
1.new Thread的弊端 执行一个异步任务你还只是如下new Thread吗? 1 2 3 4 5 6 7 8 new Thread(new Runnable() { @Override ...
- xml 操作(动态添加 property属性 其他节点同理)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...