题意

给定一个 $n$ 个整数的数列,从中至多选取 $k$ 个上升子序列(一个元素最多被选一次),使得选取的元素和最大。

分析

考虑这个问题和经典网络流问题“最长不下降子序列”相似,我们考虑对这个建图并用网络流解决。因为求得费用和,则使用费用流做法。

具体建图见代码,主要考虑拆点和建立超级源点和超级汇点。

(然后SPFA版的会超时,换成Dijkstra版的

#include<bits/stdc++.h>
using namespace std;
#define il inline typedef long long ll;
const int INF = 0x3f3f3f3f;
const int maxn = + ;
int n, k, a[maxn]; struct edge {
int to, capacity, cost, rev;
edge() {}
edge(int to, int _capacity, int _cost, int _rev) :to(to), capacity(_capacity), cost(_cost), rev(_rev) {}
};
struct Min_Cost_Max_Flow {
int V, H[maxn + ], dis[maxn + ], PreV[maxn + ], PreE[maxn + ];
vector<edge> G[maxn + ];
//调用前初始化
void Init(int n) {
V = n;
for (int i = ; i <= V; ++i)G[i].clear();
}
//加边
void Add_Edge(int from, int to, int cap, int cost) {
G[from].push_back(edge(to, cap, cost, G[to].size()));
G[to].push_back(edge(from, , -cost, G[from].size() - ));
}
//flow是自己传进去的变量,就是最后的最大流,返回的是最小费用
int Min_cost_max_flow(int s, int t, int f, int& flow) {
int res = ; fill(H, H + + V, );
while (f) {
priority_queue <pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>> > q;
fill(dis, dis + + V, INF);
dis[s] = ; q.push(pair<int, int>(, s));
while (!q.empty()) {
pair<int, int> now = q.top(); q.pop();
int v = now.second;
if (dis[v] < now.first)continue;
for (int i = ; i < G[v].size(); ++i) {
edge& e = G[v][i];
if (e.capacity > && dis[e.to] > dis[v] + e.cost + H[v] - H[e.to]) {
dis[e.to] = dis[v] + e.cost + H[v] - H[e.to];
PreV[e.to] = v;
PreE[e.to] = i;
q.push(pair<int, int>(dis[e.to], e.to));
}
}
}
if (dis[t] == INF)break;
for (int i = ; i <= V; ++i)H[i] += dis[i];
int d = f;
for (int v = t; v != s; v = PreV[v])d = min(d, G[PreV[v]][PreE[v]].capacity);
f -= d; flow += d; res += d*H[t];
for (int v = t; v != s; v = PreV[v]) {
edge& e = G[PreV[v]][PreE[v]];
e.capacity -= d;
G[v][e.rev].capacity += d;
}
}
return res;
}
int Max_cost_max_flow(int s, int t, int f, int& flow) {
int res = ;
fill(H, H + + V, );
while (f) {
priority_queue <pair<int, int>> q;
fill(dis, dis + + V, -INF);
dis[s] = ;
q.push(pair<int, int>(, s));
while (!q.empty()) {
pair<int, int> now = q.top(); q.pop();
int v = now.second;
if (dis[v] > now.first)continue;
for (int i = ; i < G[v].size(); ++i) {
edge& e = G[v][i];
if (e.capacity > && dis[e.to] < dis[v] + e.cost + H[v] - H[e.to]) {
dis[e.to] = dis[v] + e.cost + H[v] - H[e.to];
PreV[e.to] = v;
PreE[e.to] = i;
q.push(pair<int, int>(dis[e.to], e.to));
}
}
}
if (dis[t] == -INF)break;
for (int i = ; i <= V; ++i)H[i] += dis[i];
int d = f;
for (int v = t; v != s; v = PreV[v])d = min(d, G[PreV[v]][PreE[v]].capacity);
f -= d; flow += d;
res += d*H[t];
for (int v = t; v != s; v = PreV[v]) {
edge& e = G[PreV[v]][PreE[v]];
e.capacity -= d;
G[v][e.rev].capacity += d;
}
}
return res;
}
}mcmf; void solve()
{
mcmf.Init(*n+);
for(int i = ;i <= n;i++)
{
mcmf.Add_Edge(i, i+n, , -a[i]);
for(int j = i+;j <= n;j++)
{
if(a[j] >= a[i])
{
mcmf.Add_Edge(i+n, j, , ); //右边每一个大于的都要连边
//break;
}
}
}
mcmf.Add_Edge(*n+, *n+, k, );
for(int i = ;i <= n;i++) mcmf.Add_Edge(*n+, i, , ); //建立超级源点2n+1
for(int i = ;i <= n;i++) mcmf.Add_Edge(i+n, *n+, , ); //建立超级汇点2n+2
int flow = ;
int ans = mcmf.Min_cost_max_flow(*n+, *n+, INF, flow);
printf("%d\n", -ans);
} int main()
{
// freopen("multi.in", "r", stdin);
// freopen("out.txt", "w", stdout);
int T;
scanf("%d", &T);
while(T--)
{
scanf("%d%d", &n, &k);
for(int i = ;i <= n;i++) scanf("%d", &a[i]);
solve();
}
}

2019HDU多校第三场 K subsequence——最小费用最大流的更多相关文章

  1. hdu4106 区间k覆盖问题(连续m个数,最多选k个数) 最小费用最大流 建图巧妙

    /** 题目:hdu4106 区间k覆盖问题(连续m个数,最多选k个数) 最小费用最大流 建图巧妙 链接:http://acm.hdu.edu.cn/showproblem.php?pid=4106 ...

  2. 2019HDU多校第三场F Fansblog——威尔逊定理&&素数密度

    题意 给定一个整数 $P$($10^9 \leq p\leq 1^{14}$),设其前一个质数为 $Q$,求 $Q!  \ \% P$. 分析 暴力...说不定好的板子能过. 根据威尔逊定理,如果 $ ...

  3. 2019HDU多校第三场 Distribution of books 二分 + DP

    题意:给你一个序列,你可以选择序列的一个前缀,把前缀分成k个连续的部分,要求这k个部分的区间和的最大值尽量的小,问这个最小的最大值是多少? 思路:首先看到最大值的最小值,容易想到二分.对于每个二分值m ...

  4. [2019HDU多校第三场][HDU 6603][A. Azshara's deep sea]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6603 题目大意:给出一个凸包,凸包内有若干个圆,要求画尽可能多的对角线使得他们两两不在凸包内相交且不与 ...

  5. 2019Hdu多校第三场:1007 Find the answer(multiset 解法)

    原题链接: Find the answer c++中,multiset是库中一个非常有用的类型,它可以看成一个序列,插入一个数,删除一个数都能够在O(logn)的时间内完成,而且他能时刻保证序列中的数 ...

  6. 2018 HDU多校第三场赛后补题

    2018 HDU多校第三场赛后补题 从易到难来写吧,其中题意有些直接摘了Claris的,数据范围是就不标了. 如果需要可以去hdu题库里找.题号是6319 - 6331. L. Visual Cube ...

  7. 牛客多校第三场 F Planting Trees

    牛客多校第三场 F Planting Trees 题意: 求矩阵内最大值减最小值大于k的最大子矩阵的面积 题解: 矩阵压缩的技巧 因为对于我们有用的信息只有这个矩阵内的最大值和最小值 所以我们可以将一 ...

  8. 2020牛客多校第八场K题

    __int128(例题:2020牛客多校第八场K题) 题意: 有n道菜,第i道菜的利润为\(a_i\),且有\(b_i\)盘.你要按照下列要求给顾客上菜. 1.每位顾客至少有一道菜 2.给顾客上菜时, ...

  9. 牛客多校第三场 G Removing Stones(分治+线段树)

    牛客多校第三场 G Removing Stones(分治+线段树) 题意: 给你n个数,问你有多少个长度不小于2的连续子序列,使得其中最大元素不大于所有元素和的一半 题解: 分治+线段树 线段树维护最 ...

随机推荐

  1. c# sqlite 导入,升级

    导入sqlite库 1.下载nupkg 安装包 http://system.data.sqlite.org/index.html/doc/trunk/www/downloads.wiki 记得.net ...

  2. Spring Boot-日志配置(超详细)

    Spring Boot-日志配置(超详细) 更新日志: 20170810 更新通过 application.yml传递参数到 logback 中. Spring Boot-日志配置超详细 默认日志 L ...

  3. diy操作系统 附录:常用命令

    ld -m elf_i386 as --32 gcc -m 16 o

  4. c++博客转载

    C++ 中文件流(fstream)的使用方法及示例 http://blog.jobbole.com/108649/ qt中文乱码问题: https://blog.csdn.net/brave_hear ...

  5. 03 HttpServletRequest_HttpServletResponse

    HttpServletRequest:一次来自客户端的请求的相关信息 请求行 request.getMethod() 获取http请求方式 request.getRequestURI() 获取统一资源 ...

  6. (六)easyUI之对话框窗口

    一.拥有HTML的对话框 <%@ page language="java" contentType="text/html; charset=UTF-8" ...

  7. mysql 添加省市编码表

    省表格: --省级 Provincial create table Provincial(pid int,Provincial varchar(50),primary key (pid)) inser ...

  8. 关于微信小程序使用watch监听数据变化的方法

    众所周知,Vue中,可以使用监听属性 watch来观察和响应 Vue 实例上的数据变化,那么小程序能不能实现这一点呢? 监听器的原理,是将data中需监听的数据写在watch对象中,并给其提供一个方法 ...

  9. 【转载】Android性能优化之渲染篇

    下面是渲染篇章的学习笔记,欢迎大家一起学习交流! 1)Why Rendering Performance Matters 现在有不少App为了达到很华丽的视觉效果,会需要在界面上层叠很多的视图组件,但 ...

  10. MySQL全同步复制基于GR集群架构实现(Centos7)

    目录 一. 理论概述 概述 二. 部署 向组加入新节点 测试 三.总结 一. 理论概述 概述 本案例操作的是针对于MySQL的复制类型中的全同步复制,对几种复制类型简单总结下: 异步复制:MySQL默 ...