link:https://www.luogu.org/problemnew/show/P2766

题意

给定正整数序列x1,...,xn 。

(1)计算其最长不下降子序列的长度s。

(2)计算从给定的序列中最多可取出多少个长度为s的不下降子序列。

(3)如果允许在取出的序列中多次使用x1和xn,则从给定序列中最多可取出多少个长度为s的不下降子序列。

设计有效算法完成(1)(2)(3)提出的计算任务。

思路

题解来自网络流24题:

【问题分析】

第一问是LIS,动态规划求解,第二问和第三问用网络最大流解决。

【建模方法】

首先动态规划求出F[i],表示以第i位为开头的最长上升序列的长度,求出最长上升序列长度K。

1、把序列每位i拆成两个点<i.a>和<i.b>,从<i.a>到<i.b>连接一条容量为1的有向边。

2、建立附加源S和汇T,如果序列第i位有F[i]=K,从S到<i.a>连接一条容量为1的有向边。

3、如果F[i]=1,从<i.b>到T连接一条容量为1的有向边。

4、如果j>i且A[i] <= A[j]且F[j]+1=F[i],从<i.b>到<j.a>连接一条容量为1的有向边。

求网络最大流,就是第二问的结果。把边(<1.a>,<1.b>)(<N.a>,<N.b>)(S,<1.a>)(<N.b>,T)这四条边的容量修改为无穷大,再求一次网络最大流,就是第三问结果。
实际操作中,直接在原图中增加容量为inf的边即可。

【建模分析】

上述建模方法是应用了一种分层图的思想,把图每个顶点i按照F[i]的不同分为了若干层,这样图中从S出发到T的任何一条路径都是一个满足条件的最长上升子序列。

由于序列中每个点要不可重复地取出,需要把每个点拆分成两个点。单位网络的最大流就是增广路的条数,所以最大流量就是第二问结果。

第三问特殊地要求x1和xn可以重复使用,只需取消这两个点相关边的流量限制,求网络最大流即可。

#include <algorithm>
#include <iterator>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <iomanip>
#include <bitset>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <stack>
#include <cmath>
#include <queue>
#include <list>
#include <map>
#include <set>
#include <cassert> /* ⊂_ヽ
  \\ Λ_Λ 来了老弟
   \('ㅅ')
    > ⌒ヽ
   /   へ\
   /  / \\
   レ ノ   ヽ_つ
  / /
  / /|
 ( (ヽ
 | |、\
 | 丿 \ ⌒)
 | |  ) /
'ノ )  Lノ */ using namespace std;
#define lson (l , mid , rt << 1)
#define rson (mid + 1 , r , rt << 1 | 1)
#define debug(x) cerr << #x << " = " << x << "\n";
#define pb push_back
#define pq priority_queue typedef long long ll;
typedef unsigned long long ull;
//typedef __int128 bll;
typedef pair<ll ,ll > pll;
typedef pair<int ,int > pii;
typedef pair<int,pii> p3; //priority_queue<int> q;//这是一个大根堆q
//priority_queue<int,vector<int>,greater<int> >q;//这是一个小根堆q
#define fi first
#define se second
//#define endl '\n' #define boost ios::sync_with_stdio(false);cin.tie(0)
#define rep(a, b, c) for(int a = (b); a <= (c); ++ a)
#define max3(a,b,c) max(max(a,b), c);
#define min3(a,b,c) min(min(a,b), c); const ll oo = 1ll<<;
const ll mos = 0x7FFFFFFF; //
const ll nmos = 0x80000000; //-2147483648
const int inf = 0x3f3f3f3f;
const ll inff = 0x3f3f3f3f3f3f3f3f; //
const int mod = 1e9+;
const double esp = 1e-;
const double PI=acos(-1.0);
const double PHI=0.61803399; //黄金分割点
const double tPHI=0.38196601; template<typename T>
inline T read(T&x){
x=;int f=;char ch=getchar();
while (ch<''||ch>'') f|=(ch=='-'),ch=getchar();
while (ch>=''&&ch<='') x=x*+ch-'',ch=getchar();
return x=f?-x:x;
} inline void cmax(int &x,int y){if(x<y)x=y;}
inline void cmax(ll &x,ll y){if(x<y)x=y;}
inline void cmin(int &x,int y){if(x>y)x=y;}
inline void cmin(ll &x,ll y){if(x>y)x=y;} /*-----------------------showtime----------------------*/ const int maxn = ;
int a[maxn],dp[maxn]; struct E{
int u,v,w;
int nxt;
}edge[*maxn*maxn];
int head[maxn*],gtot = ;
void addedge(int u,int v,int w){
edge[gtot].u = u;
edge[gtot].v = v;
edge[gtot].w = w;
edge[gtot].nxt = head[u];
head[u] = gtot++; edge[gtot].u = v;
edge[gtot].v = u;
edge[gtot].w = ;
edge[gtot].nxt = head[v];
head[v] = gtot++;
} int dis[maxn*],cur[maxn*];
bool bfs(int s,int t){
memset(dis, inf, sizeof(dis));
for(int i=s; i<=t; i++) cur[i] = head[i]; dis[s] = ;
queue<int>que; que.push(s);
while(!que.empty()){
int u = que.front(); que.pop();
for(int i=head[u]; ~i; i = edge[i].nxt){
int v = edge[i].v, w = edge[i].w;
if(w > && dis[v] > dis[u] + ){
dis[v] = dis[u] + ;
que.push(v);
}
}
}
return dis[t] < inf;
} int dfs(int u,int t,int maxflow){
if(u == t || maxflow == ) return maxflow; for(int i=cur[u]; ~i; i = edge[i].nxt){
cur[u] = i;
int v = edge[i].v, w = edge[i].w;
if(w > && dis[v] == dis[u] + ){
int f = dfs(v, t, min(w, maxflow));
if(f > ) {
edge[i].w -= f;
edge[i^].w += f;
return f;
}
}
}
return ;
} int dinic(int s,int t){
int flow = ;
while(bfs(s, t)){
while(int f = dfs(s, t, inf)) flow += f;
}
return flow;
}
int main(){
memset(head, -, sizeof(head));
int n, k = ; scanf("%d", &n);
rep(i, , n) scanf("%d", &a[i]); rep(i, , n){
int mx = ;
for(int j=; j<i; j++) if(a[j] <= a[i]) cmax(mx, dp[j]);
dp[i] = mx + ;
cmax(k, dp[i]);
}
printf("%d\n", k); int s = , t = n + n + ; rep(i, , n){
addedge(i, i+n, );
if(dp[i] == k) addedge(i+n, t, );
if(dp[i] == ) addedge(s, i, );
} for(int i=; i<=n; i++){
for(int j=i+; j<=n; j++){
if(a[i] <= a[j]&&dp[j] == dp[i] + ) addedge(i+n, j, );
//这里注意还要判断a【i】 <= a【j】
}
}
int res = dinic(s,t);
printf("%d\n", res); if(dp[] == )
addedge(s, , inf),addedge(,+n,inf);
if(dp[n] == k)
addedge(n,n+n,inf),addedge(n+n,t,inf); printf("%d\n", dinic(s, t) + res);
return ;
}

P2766 最长不下降子序列问题 网络流的更多相关文章

  1. P2766 最长不下降子序列问题 网络流重温

    P2766 最长不下降子序列问题 这个题目还是比较简单的,第一问就是LIS 第二问和第三问都是网络流. 第二问要怎么用网络流写呢,首先,每一个只能用一次,所以要拆点. 其次,我们求的是长度为s的不下降 ...

  2. 洛谷P2766 最长不下降子序列问题 网络流_DP

    Code: #include<cstdio> #include<iostream> #include<vector> #include<algorithm&g ...

  3. 【24题】P2766最长不下降子序列问题

    网络流二十四题 网络流是个好东西,希望我也会. 网络流?\(orz\ zsy!!!!!\) P2766 最长不下降子序列问题 考虑我们是如何\(dp\)这个\(LIS\)的. 我们是倒着推,设置\(d ...

  4. [**P2766** 最长不下降子序列问题](https://www.luogu.org/problemnew/show/P2766)

    P2766 最长不下降子序列问题 考虑我们是如何\(dp\)这个\(LIS\)的. 我们是倒着推,设置\(dp(i)\)代表以\(i\)为起点的\(LIS\)是多少.转移太显然了 \[ dp(i)=m ...

  5. 【Luogu】P2766最长不下降子序列问题(暴力网络流)

    题目链接 水题qwq,数据都那么水. 我要是出数据的人我就卡$n^3$建图. qwq. 然而这么水的题我!居!然!没!有!1!A!!还!提!交!了!五!遍!!! md从现在开始要锻炼1A率了 看我从今 ...

  6. P2766 最长不下降子序列问题 题解(网络流)

    题目链接 最长不下降子序列问题 解题思路 分成三小问解决. 第一小问,求\(LIS\),因为\(n<=500\),直接\(O(N^2)\)暴力求解即可. 第二三小问,建立模型用网络流求解. 对于 ...

  7. 网络流 之 P2766 最长不下降子序列问题

    题目描述 «问题描述: 给定正整数序列x1,...,xn . (1)计算其最长不下降子序列的长度s. (2)计算从给定的序列中最多可取出多少个长度为s的不下降子序列. (3)如果允许在取出的序列中多次 ...

  8. 网络流24题 P2766 最长不下降子序列问题

    题目描述 «问题描述: 给定正整数序列x1,...,xn . (1)计算其最长不下降子序列的长度s. (2)计算从给定的序列中最多可取出多少个长度为s的不下降子序列. (3)如果允许在取出的序列中多次 ...

  9. 洛谷 P2766 最长不下降子序列问【dp+最大流】

    死于开小数组的WA?! 第一问n方dp瞎搞一下就成,f[i]记录以i结尾的最长不下降子序列.记答案为mx 第二问网络流,拆点限制流量,s向所有f[i]为1的点建(s,i,1),所有f[i]为mx(i+ ...

随机推荐

  1. the license has been canceled

    ideal 的 注册码并没有失效,却显示这个信息 the license has been canceled 如果用的是Windows系统,在hosts文件添加下边的ip及映射 0.0.0.0 acc ...

  2. C# sql 批量插入数据库的语句

    //执行DataTable数据导入 public static int UpdateDt(string strConn, DataTable dt) { try { string tablaName ...

  3. let和const解构赋值

    1.let和const:最基础也很容易理解的,let是 声明一个变量,const是声明一个常量. 具体细节看如下实例代码 { let a=; console.log(a) } // console.l ...

  4. 一个C++的ElasticSearch Client

    ElasticSearch官方是没有提供C++的client的:因此决定自己写一个,命名为ESClient https://github.com/ATinyAnt/ESClient(手下留星 star ...

  5. 曹工杂谈:Linux服务器上,Spring Boot 原地修改 jar 包配置文件/替换class文件,免去重复上传的麻烦

    一.前言 相信很多同学有这样的需求,现在很多公司都有多地的研发中心,经常需要跨地区部署,比如,博主人在成都,但是服务器是北京的.一般城市间网络都不怎么好,上传一个几十兆的jar包那是真的慢,别说现在微 ...

  6. 浅析java中的语法糖

    概述 编译器是一种计算机程序, 它主要的目的是将便于人编写.阅读.维护的高级计算机语言所写的源代码程序, 翻译为计算机能解读.运行的低阶机器语言的程序, 即可执行文件.而 javac 就是java语言 ...

  7. 7z 命令行方式生成自解压exe

    一.下载 7z是一个免费的工具,除了通过命令行的方式提供各种文件.压缩包相关的操作外,还提供了一种方式可以打出自解压的exe程序.该程序从运行到结束经历了三个流程: (1) 解压文件到用户临时目录: ...

  8. spring注解不支持静态变量注入

    spring注解不支持静态变量注入:今天敲代码  自动配置 配置: Animal.java package study01_autoconfig.beanConfig; import org.spri ...

  9. git的使用(一)

    git   —version  展示git的版本 tanya ~$ git --version git version 2.22.0 最小配置   git config —global user.na ...

  10. 前端插件之Datatables使用--上篇

    工欲善其事,必先利其器 本系列文章介绍我在运维系统开发过程中用到的那些顺手的前端插件,前边两篇分别介绍了Duallistbox插件和Select2插件的使用,这一篇开始Databases的征服之旅 D ...