LOJ6005 「网络流 24 题
来源:互联网 发布:淘宝卖盗版软件违法吗 编辑:程序博客网 时间:2024/06/05 21:03
大家都很强, 可与之共勉 。
题意:
给定正整数序列
- 计算其最长递增子序列的长度
s 。 - 计算从给定的序列中最多可取出多少个长度为
s 的递增子序列。 - 如果允许在取出的序列中多次使用
x1 和xn ,则从给定序列中最多可取出多少个长度为s 的递增子序列。
题解:
我忘了
第一问即是裸题,直接单调栈优化的最长上升子序列求解就可以了。
第二问用网络流,考虑如何建边才能使得每一条路径都是长度为
我们已经预处理出了
f[i] 数组表示以第i 个结尾的最长不递减子序列的长度。设第一问的答案为k 。
然后拆点,每个点a ,拆为(a1,a2) ,然后a1向a2 连一条容量为1 的弧,保证每个点一定只使用一次。
建立源点S ,汇点T 。
其中对于f[u]=1 的点,S 向u1 连一条容量为1 的弧,对于f[v]=k 的点,v2 向T 连一条容量为1 的弧。
然后对于i>j且a[j]≤a[i] ,我们连一条j2→i1 ,流量为1 的边。
然后跑最大流就好了,一定保证从
第三问在第二问的基础上完成,我们发现要求的只是
就保证了正确性。注意一些细节
# include <bits/stdc++.h># define N 5005class Network {private : struct edge { int to, w, nxt ; edge ( ) { } edge ( int to, int w, int nxt ) : to ( to ), w ( w ), nxt ( nxt ) { } } g [60010 << 1] ; int head [N], cur [N], ecnt ; int S, T , dep [N] ; inline int dfs ( int u, int a ) { if ( u == T || ! a ) return a ; int flow = 0, v, f ; for ( int& i = cur [u] ; i ; i = g [i].nxt ) { v = g [i].to ; if ( dep [v] == dep [u] + 1 ) { f = dfs ( v, std :: min ( g [i].w, a - flow ) ) ; g [i].w -= f, g [i ^ 1].w += f ; flow += f ; if ( a == flow ) return a ; } } if ( ! flow ) dep [u] = -1 ; return flow ; } inline bool bfs ( int S, int T ) { static std :: queue < int > q ; memset ( dep, 0, sizeof ( int ) * ( T + 1 ) ) ; dep [S] = 1 ; q.push ( S ) ; while ( ! q.empty ( ) ) { int u = q.front ( ) ; q.pop ( ) ; for ( int i = head [u] ; i ; i = g [i].nxt ) { int& v = g [i].to ; if ( g [i].w && ! dep [v] ) { dep [v] = dep [u] + 1 ; q.push ( v ) ; } } } return dep [T] ; }public : Network ( ) { ecnt = 1 ; } inline void add_edge ( int u, int v, int w ) { g [++ ecnt] = edge ( v, w, head [u] ) ; head [u] = ecnt ; g [++ ecnt] = edge ( u, 0, head [v] ) ; head [v] = ecnt ; } inline void clear ( ) { ecnt = 1 ; memset ( head, 0, sizeof head ) ; } inline int dinic ( int S, int T ) { this -> S = S, this -> T = T ; int rt = 0 ; while ( bfs ( S, T ) ) { memcpy ( cur, head, sizeof ( int ) * ( T + 1 ) ) ; rt += dfs ( S, 0x3f3f3f3f ) ; } return rt ; }} Lazer ;int f [N] ;inline int Lis ( int* s, int n ) { if ( n == 0 ) return 0 ; int* src = new int [( const int ) n + 1] ; int len ( 1 ) ; src [1] = s [1] ; f [1] = 1 ; for ( int i = 2 ; i <= n ; ++ i ) { src [f [i] = ( s [i] >= src [len] ) ? ( ++ len ) : ( std :: upper_bound ( src + 1, src + 1 + len, s [i] ) - src )] = s [i] ; } return len ;}int a [N] ;int main ( ) { int n ; scanf ( "%d", & n ) ; for ( int i = 1 ; i <= n ; ++ i ) { scanf ( "%d", a + i ) ; } int ans = Lis ( a, n ) ; printf ( "%d\n", ans ) ; const int S = n * 2 + 1, T = n * 2 + 2 ; for ( int i = 1 ; i <= n ; ++ i ) { if ( f [i] == ans ) { Lazer.add_edge ( i + n, T, 1 ) ; } // no else if ( f [i] == 1 ) { Lazer.add_edge ( S, i, 1 ) ; } Lazer.add_edge ( i, i + n, 1 ) ; } for ( int i = 1 ; i <= n ; ++ i ) for ( int j = 1 ; j < i ; ++ j ) if ( f [i] == f [j] + 1 && a [i] >= a [j] ) { Lazer.add_edge ( j + n, i, 1 ) ; } printf ( "%d\n", Lazer.dinic ( S, T ) ) ; Lazer.clear ( ) ; for ( int i = 1 ; i <= n ; ++ i ) { if ( f [i] == ans ) { Lazer.add_edge ( i + n, T, i == n ? 0x3f3f3f3f : 1 ) ; } if ( f [i] == 1 ) { // no else Lazer.add_edge ( S, i, i == 1 ? 0x3f3f3f3f : 1 ) ; } Lazer.add_edge ( i, i + n, i == 1 || i == n ? 0x3f3f3f3f : 1 ) ; } for ( int i = 1 ; i <= n ; ++ i ) for ( int j = 1 ; j < i ; ++ j ) if ( f [i] == f [j] + 1 && a [i] >= a [j] ) { Lazer.add_edge ( j + n, i, 1 ) ; } printf ( "%d\n", Lazer.dinic ( S, T ) ) ; return 0 ;}
阅读全文
0 0
- LOJ6005 「网络流 24 题
- LOJ6005 网络流24题
- loj6005「网络流 24 题」最长递增子序列(dp+最大流)
- 【网络流】网络流24题
- [网络流]: 网络流24题
- 网络流24题
- 网络流24题
- 网络流24题
- 网络流24题
- 「网络流 24 题」试题库
- 「网络流 24 题」试题库
- LOJ6000 「网络流 24 题
- LOJ6001 「网络流 24 题
- LOJ6002 「网络流 24 题
- LOJ6003 「网络流 24 题
- LOJ6004 「网络流 24 题
- LOJ6006「网络流 24 题
- LOJ6007 「网络流 24 题
- 集成极光推送总结
- Invalidate()与UpdateWindow()
- 判断子线程是否执行完毕
- redis sentinel配置(windows环境)
- 第七周 项目 4
- LOJ6005 「网络流 24 题
- JDBC与MySQL
- 第四周-项目三-单链表:逆置、连接与递增判断
- 如何给html头部的title添加icon
- 第十五周 项目2-大数据集上排序算法性能的体验
- 如何设置 HSTS 头字段
- ES6爬坑之路之块级作用域
- 详解SpringBoot配置devtools实现热部署
- 《揭秘Angular 2》-TypeScript声明和解构