网络流24题6. 最长递增子序列问题
来源:互联网 发布:flotherm软件下载 编辑:程序博客网 时间:2024/05/22 04:37
最长递增子序列问题
Description
给定正整数序列
(1)计算其最长递增子序列的长度 s。
(2)计算从给定的序列中最多可取出多少个长度为 s 的递增子序列。
(3)如果允许在取出的序列中多次使用
Input
第 1 行有 1 个正整数 n,表示给定序列的长度。接下来的 1 行有 n 个正整数
Output
第 1 行是最长递增子序列的长度 s。第 2 行是可取出的长度为 s 的递增子序列个数。第 3 行是允许在取出的序列中多次使用
题解
题面冠冕堂皇的说着什么最长递增子序列,数据竟给的是最长不上升子序列的,令人无语。
第一问可以用最简单的
下面考虑建图,因为每个
然后从s点向每个
对于第三问只需要将
【建模分析】
上述建模方法是应用了一种分层图的思想,把图每个顶点i按照
#include<cstdio>#include<cstring>#include<iostream>using namespace std;const int N = 1000 + 10, M = 2000000 + 10, inf = 0x3f3f3f3f;struct Edge{ int fr, to, cap, flow;}edg[M];int hd[N], nxt[M];int d[N], vis[N], q[N], dfn;int s, t;int n, ans, tot, k;int a[N], f[N];int e1, e2, e3, e4;void insert(int u, int v, int w){ edg[tot].fr = u, edg[tot].to = v, edg[tot].cap = w; nxt[tot] = hd[u], hd[u] = tot; if(u == 1 && v == n + 1) e1 = tot; if(u == n && v == n + n) e2 = tot; if(u == s && v == 1) e3 = tot; if(u == n + n && v == t) e4 = tot; tot++; edg[tot].fr = v, edg[tot].to = u; nxt[tot] = hd[v], hd[v] = tot; tot++;}bool bfs(){ int head = 1, tail = 1; q[1] = s; vis[s] = ++dfn; d[s] = 0; while(head <= tail){ int u = q[head++]; for(int i = hd[u]; i >= 0; i = nxt[i]){ Edge &e = edg[i]; if(vis[e.to] == dfn || e.cap <= e.flow) continue; vis[e.to] = dfn; d[e.to] = d[u] + 1; q[++tail] = e.to; } } return vis[t] == dfn;}int dfs(int x, int a){ if(x == t || a == 0) return a; int flow = 0, f; for(int i = hd[x]; i >= 0; i = nxt[i]){ Edge &e = edg[i]; if(d[e.to] == d[x] + 1 && (f = dfs(e.to, min(a, e.cap - e.flow))) > 0){ flow += f; e.flow += f; edg[i^1].flow -= f; a -= f; if(a == 0) break; } } return flow;}void dp(){ for(int i = 1; i <= n; i++){ for(int j = 1; j < i; j++) if(a[j] <= a[i] && f[j] > f[i]) f[i] = f[j]; f[i]++; k = max(k, f[i]); }}void build(){ s = 0, t = n * 2 + 1; int tmp[5]; for(int i = 1; i <= n; i++){ insert(i, i + n, 1); if(f[i] == 1) insert(s, i, 1); if(f[i] == k) insert(i + n, t, 1); } for(int i = 1; i <= n; i++) for(int j = i + 1; j <= n; j++) if(a[i] <= a[j] && f[i] + 1 == f[j]) insert(i + n, j, 1);}void init(){ memset(hd, -1, sizeof(hd)); scanf("%d", &n); for(int i = 1; i <= n; i++) scanf("%d", &a[i]);}void work(){ dp(); printf("%d\n", k); build(); while(bfs()) ans += dfs(s, inf); printf("%d\n", ans); edg[e1].cap = edg[e2].cap = edg[e3].cap = edg[e4].cap = inf; while(bfs()) ans += dfs(s, inf); printf("%d\n", ans);}int main(){ init(); work(); return 0;}
- 网络流24题6. 最长递增子序列问题
- [网络流24题 #6]最长递增子序列问题
- 网络流24题之最长递增子序列问题
- 【网络流24题】最长递增子序列问题
- 【codevs1906】[网络流24题]最长递增子序列问题
- 网络流24题:最长递增子序列问题
- 网络流24题之六 最长递增子序列
- 网络流24题6 最长递增子序列
- COGS 731. [网络流24题] 最长递增子序列
- 「网络流 24 题」最长递增子序列
- 「网络流 24 题」最长递增子序列
- [网络流24题] 最长递增子序列问题 最大流/
- 线性规划与网络流24题 06最长递增子序列问题
- 网络流与线性规划24题06最长递增子序列问题
- kyeremal-网络流24题T6-最长递增子序列问题
- nefu487最长递增子序列问题【网络流24题】超详细讲解+模板
- code vs [网络流24题] 最长递增子序列问题
- 最长递增子序列问题[网络流24题之6]
- VC视图文档结构
- 1119. Pre- and Post-order Traversals (****推荐*****)
- 又一次闲来无聊,使用Java的动态代理机制来模拟AOP
- eclipse 项目遇到的问题
- 开发工具链(国内项目)(持续更)
- 网络流24题6. 最长递增子序列问题
- js代码判断浏览器种类IE、FF、Opera、Safari、chrome及IE版本
- 常见svn图标的含义(修改版)
- Nodejs之地址解析模块URL
- HSLF操作PPT
- hadoop2.7.3学习之WordCount疑问
- Markdown编辑器使用的一些技巧
- [UVA] 10976 Fractions Again?!
- Linux下使用nexus搭建maven私服