LOJ 3158: 「NOI2019」序列
题目传送门:LOJ #3158。
题意简述:
给定两个长度为 \(n\) 的正整数序列 \(a,b\),要求在每个序列中都选中 \(K\) 个下标,并且要保证同时在两个序列中都被选中的下标至少有 \(L\) 个,使得选中的下标对应的数的总和最大。
题解:
题目相当于要求在两个序列中选出 \(K\) 对数,不妨一对一对地选。
有个结论是说,上一步的最优决策一定不会再反悔,就是已经选的不会再撤销。
然后做完了,用堆维护一些东西,精细实现就好了。
下面是代码,复杂度 \(\mathcal{O}\left(\sum n\log n\right)\)。
#include <cstdio>
#include <algorithm>
#include <queue>
#include <cctype>
#define fi first
#define se second
#define mp std::make_pair
typedef long long LL;
typedef std::pair<int, int> pii;
typedef std::priority_queue<pii> PQ;
const int MN = 200005;
inline void read(int &x) {
x = 0; static char ch;
while (!isdigit(ch = getchar())) ;
while (x = x * 10 + (ch & 15), isdigit(ch = getchar()));
}
inline void write(LL x) {
static char st[15];
static char *t = st;
while (x) *t++ = (x % 10) | 48, x /= 10;
while (t != st) putchar(*--t);
putchar('\n');
}
int N, K, L;
int A[MN], B[MN];
bool vA[MN], vB[MN];
PQ A0, B0, A1, B1, C;
LL Ans;
int main() {
freopen("sequence.in", "r", stdin);
freopen("sequence.out", "w", stdout);
int T; scanf("%d", &T);
while (T--) {
Ans = 0;
while (!A0.empty()) A0.pop();
while (!B0.empty()) B0.pop();
while (!A1.empty()) A1.pop();
while (!B1.empty()) B1.pop();
while (!C.empty()) C.pop();
read(N), read(K), read(L);
for (int i = 1; i <= N; ++i) read(A[i]);
for (int i = 1; i <= N; ++i) read(B[i]);
for (int i = 1; i <= N; ++i) {
vA[i] = vB[i] = 0;
A0.push(mp(A[i], i));
B0.push(mp(B[i], i));
C.push(mp(A[i] + B[i], i));
}
int left = K - L;
for (int cnt = 1; cnt <= K; ++cnt) {
while (vA[A0.top().se]) A0.pop();
while (vB[B0.top().se]) B0.pop();
if (left) {
pii a = A0.top(), b = B0.top();
A0.pop(), B0.pop();
Ans += a.fi + b.fi;
int ap = a.se, bp = b.se;
vA[ap] = vB[bp] = 1;
if (ap == bp) C.pop();
else {
--left;
if (vB[ap]) ++left, A1.pop();
else B1.push(mp(B[ap], ap));
if (vA[bp]) ++left, B1.pop();
else A1.push(mp(A[bp], bp));
}
}
else {
pii c0 = C.empty() ? mp(0, 0) : C.top();
while (vA[c0.se] || vB[c0.se])
C.pop(), c0 = C.empty() ? mp(0, 0) : C.top();
pii a0 = A0.top(), b0 = B0.top();
pii a1 = A1.empty() ? mp(-b0.fi, 0) : A1.top();
pii b1 = B1.empty() ? mp(-a0.fi, 0) : B1.top();
int a = a1.fi + b0.fi;
int b = b1.fi + a0.fi;
if (c0.fi >= a && c0.fi >= b)
Ans += c0.fi, vA[c0.se] = vB[c0.se] = 1;
else if (a >= b) {
Ans += a;
A1.pop(), B0.pop(), vA[a1.se] = vB[b0.se] = 1;
if (b1.se == b0.se) B1.pop(), ++left;
else A1.push(mp(A[b0.se], b0.se));
}
else {
Ans += b;
B1.pop(), A0.pop(), vB[b1.se] = vA[a0.se] = 1;
if (a1.se == a0.se) A1.pop(), ++left;
else B1.push(mp(B[a0.se], a0.se));
}
}
} write(Ans);
}
return 0;
}
证明?不要问我证明啊!其实仔细分析一下应该是能证出来的,再不行就先套个费用流模型。
LOJ 3158: 「NOI2019」序列的更多相关文章
- Loj #3059. 「HNOI2019」序列
Loj #3059. 「HNOI2019」序列 给定一个长度为 \(n\) 的序列 \(A_1, \ldots , A_n\),以及 \(m\) 个操作,每个操作将一个 \(A_i\) 修改为 \(k ...
- loj #2051. 「HNOI2016」序列
#2051. 「HNOI2016」序列 题目描述 给定长度为 n nn 的序列:a1,a2,⋯,an a_1, a_2, \cdots , a_na1,a2,⋯,an,记为 a[1: ...
- LOJ 3160: 「NOI2019」斗主地
题目传送门:LOJ #3160. 简要题意: 有一个长度为 \(n\) 的序列 \(a\),初始时 \(a_i=i\) 或 \(a_i=i^2\),这取决于 \(\mathrm{type}\) 的值. ...
- LOJ #2183「SDOI2015」序列统计
有好多好玩的知识点 LOJ 题意:在集合中选$ n$个元素(可重复选)使得乘积模$ m$为$ x$,求方案数对$ 1004535809$取模 $ n<=10^9,m<=8000且是质数,集 ...
- LOJ 3059 「HNOI2019」序列——贪心与前后缀的思路+线段树上二分
题目:https://loj.ac/problem/3059 一段 A 选一个 B 的话, B 是这段 A 的平均值.因为 \( \sum (A_i-B)^2 = \sum A_i^2 - 2*B \ ...
- LOJ 3159: 「NOI2019」弹跳
题目传送门:LOJ #3159. 题意简述: 二维平面上有 \(n\) 个整点,给定每个整点的坐标 \((x_i,y_i)\). 有 \(m\) 种边,第 \(i\) 种边从 \(p_i\) 号点连向 ...
- LOJ 3156: 「NOI2019」回家路线
题目传送门:LOJ #3156. 题意简述: 有一张 \(n\) 个点 \(m\) 条边的有向图,边有两个权值 \(p_i\) 和 \(q_i\)(\(p_i<q_i\))表示若 \(p_i\) ...
- 「NOI2019」序列
NKOJ卡常卡不过QAQ description 给两个A,B序列,让你分别在A,B中各选k个数,其中至少有L对下标相等. Solution 把问题转化为至多选n-K对下标不同的对. 配对问题就用费用 ...
- loj#2002. 「SDOI2017」序列计数(dp 矩阵乘法)
题意 题目链接 Sol 质数的限制并没有什么卵用,直接容斥一下:答案 = 忽略质数总的方案 - 没有质数的方案 那么直接dp,设\(f[i][j]\)表示到第i个位置,当前和为j的方案数 \(f[i ...
随机推荐
- [LeetCode] 148. Sort List 链表排序
Sort a linked list in O(n log n) time using constant space complexity. Example 1: Input: 4->2-> ...
- C# 方法递归
一.简介 方法递归就是自己调用自己. 未完结
- IntelliJ IDEA 提交代码时出现:Code analysis failed with exception: com.intellij.psi......
IntelliJ IDEA 提交代码时出现:Code analysis failed with exception: com.intellij.psi...... 错误原因: 当我们勾选Perform ...
- HTTP之网关的分类
网关的分类 ========================摘自<HTTP权威指南>============================= 1. HTTP/*:服务器端Web网关 请 ...
- 在linux上安装taiga
# taiga 安装配置 1.简介 本文档介绍了如何部署完整的Taiga服务(每个模块都是Taiga平台的一部分). Taiga平台由三个主要组件组成,每个组件在编译时和运行时都有自己的依赖关系: t ...
- 动态引用存储——集合&&精确的集合定义——泛型
1,集合宏观理解 1.1,为什么引入集合? 对于面向对象的语言来说,操作对象的功能不可或缺. 为了方便对对象进行操作和处理,就必须要对对象进行暂时的存储.[数据最终存在数据库里] 使用数组来存储对象的 ...
- POJ 1061 青蛙的约会 题解
青蛙的约会 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 139755 Accepted: 31803 Descript ...
- mysql备份、还原数据库(命令行)
这里记录下MySQL如何通过命令行备份和还原数据库. 简单的三个步骤 方法很简单,可以分为三个步骤: 1.打开cmd控制台(命令行). 2.输入相应命令完成备份还原操作. 3.关闭cmd控制台. 就和 ...
- Linux(centOS6.5)安装RabbitMQ
第一.下载erlang和rabbitmq-server的rpm: wget http://www.rabbitmq.com/releases/erlang/erlang-19.0.4-1.el7.c ...
- java8 Lambda 表达式和函数式接口快速理解
前言 接上篇文章 java8 新特性 由于上篇过于庞大,使得重点不够清晰,本篇单独拿出 java8 的 Lambda 表达式和函数式接口说明. Lambda 表达式 lambda 表达式其实就是使用了 ...