洛谷P2766 最长不下降子序列问题 网络流_DP
Code:
#include<cstdio>
#include<iostream>
#include<vector>
#include<algorithm>
#include<queue>
#include<cstring>
using namespace std;
const int INF=1000000;
const int maxn=600;
# define pb push_back
int A[maxn],f[maxn];
int s,t;
int ans=0;
struct Edge{
int from,to,cap;
Edge(int u,int v,int c):from(u),to(v),cap(c) {}
};
struct Dicnic{
vector<Edge>edges;
vector<int>G[maxn];
int d[maxn],vis[maxn],cur[maxn];
queue<int>Q;
void addedge(int u,int v,int c){
edges.pb(Edge(u,v,c)); //正向弧
edges.pb(Edge(v,u,0)); //反向弧
int m=edges.size();
G[u].pb(m-2);
G[v].pb(m-1);
}
int BFS()
{
memset(vis,0,sizeof(vis));
d[s]=0,vis[s]=1;Q.push(s);
while(!Q.empty()){
int u=Q.front();Q.pop();
int sz=G[u].size();
for(int i=0;i<sz;++i){
Edge e=edges[G[u][i]];
if(!vis[e.to]&&e.cap>0){
d[e.to]=d[u]+1,vis[e.to]=1;
Q.push(e.to);
}
}
}
return vis[t];
}
int dfs(int x,int a){
if(x==t)return a;
int sz=G[x].size();
int f,flow=0;
for(int i=cur[x];i<sz;++i){
Edge e=edges[G[x][i]];
cur[x]=i;
if(d[e.to]==d[x]+1&&e.cap>0){
f=dfs(e.to,min(a,e.cap));
if(f)
{
int u=G[x][i];
a-=f;
edges[u].cap-=f;
edges[u^1].cap+=f;
flow+=f;
if(a==0)break;
}
}
}
return flow;
}
int maxflow(){
while(BFS()){
memset(cur,0,sizeof(cur));
ans+=dfs(s,INF);
}
return ans;
}
}op;
int main()
{ int n;cin>>n;
for(int i=1;i<=n;++i)cin>>A[i];
f[1]=1;
int anss=1;
for(int i=2;i<=n;++i){
f[i]=1;
for(int j=1;j<i;++j)
if(A[j]<=A[i]){f[i]=max(f[i],f[j]+1);anss=max(anss,f[i]);}
}
s=0,t=510;
for(int i=1;i<=n;++i){
if(f[i]==1)op.addedge(s,i,1);
if(f[i]==anss)op.addedge(i,t,1);
}
for(int i=1;i<n;++i)
for(int j=i+1;j<=n;++j)if(f[j]==f[i]+1&&A[j]>=A[i])op.addedge(i,j,1);
cout<<anss<<endl;
cout<<op.maxflow()<<endl;
if(anss==1){cout<<n<<endl;return 0;}
if(f[1]==anss)op.addedge(1,t,INF);
if(f[n]==anss)op.addedge(n,t,INF); //出边
op.addedge(s,1,INF);
if(f[n]==1)op.addedge(s,n,INF); //入边
for(int i=2;i<=n;++i)
{
if(A[i]>=A[1]&&f[i]==f[1]+1)op.addedge(1,i,INF);
}
int fin=op.maxflow();
if(fin>=INF)
{
int t=fin/INF; fin=(fin+t)%INF;
}
cout<<fin;
return 0;
}
洛谷P2766 最长不下降子序列问题 网络流_DP的更多相关文章
- 洛谷P2766 最长不下降子序列问题(最大流)
传送门 第一问直接$dp$解决,求出$len$ 然后用$f[i]$表示以$i$为结尾的最长不下降子序列长度,把每一个点拆成$A_i,B_i$两个点,然后从$A_i$向$B_i$连容量为$1$的边 然后 ...
- 洛谷 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 最长不下降子序列问题 网络流重温
P2766 最长不下降子序列问题 这个题目还是比较简单的,第一问就是LIS 第二问和第三问都是网络流. 第二问要怎么用网络流写呢,首先,每一个只能用一次,所以要拆点. 其次,我们求的是长度为s的不下降 ...
- P2766 最长不下降子序列问题 网络流
link:https://www.luogu.org/problemnew/show/P2766 题意 给定正整数序列x1,...,xn . (1)计算其最长不下降子序列的长度s. (2)计算从给定的 ...
- 洛谷.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 最长递增子序列问题
https://www.luogu.org/problemnew/show/P2766 注:题目描述有误,本题求的是最长不下降子序列 方案无限多时输出 n 网络流求方案数,长见识了 第一问: DP 同 ...
随机推荐
- 【摘录】JAVA内存管理-评估垃圾收集性能的工具
第七章 评估垃圾收集性能的工具 各种各样的诊断和监视工具可以用来评估垃圾收集性能.本章简要概述他们中的几个.可以通过第九章中的“Tools and Troubleshooting”链接获得更多的信息. ...
- elasticsearch集群添加节点
最简配置文件: cluster.name: your_cluster_name node.name: your_ip network.host: 0.0.0.0 http.port: your_p ...
- ZBrush中Nudge推动笔刷介绍
本文我们来介绍Nudge推动笔刷,该笔刷在使用时能够产生旋转涂抹的效果,Nudge笔刷允许您在模型表面移动顶点,而这些移动的顶点仍然停留在模型的原来的表面,它与Move笔刷还是不同的,利用Move笔刷 ...
- springboot中文文档
http://blog.geekidentity.com/spring/spring_boot_translation/ https://blog.csdn.net/zfrong/article/de ...
- webpack——打包JS
1.在文件中打开命令行,输入code ./ (我的编译器是vs code) 2.然后会弹出编译器,在编译器内新建js文件app,sum app.js import sum from './sum ...
- VUE:事件处理和表单输入绑定
事件处理 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <titl ...
- [Angular] Send Data via HTTP using Angular HttpParams
Obviously in a real world application we do not only fetch data from the backend, but we also send d ...
- 数组溢界地址的正确使用: 即 int a[6] 中的 a[-1] 和 a[6] 正确使用
正如大家所知道的那样: 数组 int a[6] , 编译器阅读到这句数组定义,会为分配6个int 类型的地址:a[0] a[1] a[2] a[3] a[4] a[5].我们 能够正 ...
- hdu Escape
Escape 题目: 非常裸的多重匹配. 可是点数较多,所以要用到状态压缩. . .. .. 第一次写. 好厉害的赶脚. #include <iostream> #include < ...
- JUnit4.8.2源码分析-4 RunNotifier与RunListener
JUnit4运行过程中,org.junit.runner.notification. RunListener和RunNotifier运用了观察者模式. 1.观察者 观察者Observer/Listen ...