「POJ Challenge」生日礼物
Tag
堆,贪心,链表
Solution
把连续的符号相同的数缩成一个数,去掉两端的非正数,得到一个正负交替的序列,把该序列中所有数的绝对值扔进堆中,用所有正数的和减去一个最小值,这个最小值的求法与「CTSC 2007 数据备份」相同。
Code
#include <cstdio>
#include <queue>
const int N = 100005, inf = 0x3f3f3f3f;
struct Node {
int x, y;
bool operator < (const Node &rhs) const {
return x > rhs.x;
}
};
int a[N], L[N], R[N], vis[N];
std::priority_queue<Node> Q;
int read() {
int x = 0, f = 1;
char c = getchar();
while (c < '0' || c > '9') {
if (c == '-') f = -1;
c = getchar();
}
while (c >= '0' && c <= '9') {
x = (x << 3) + (x << 1) + (c ^ 48);
c = getchar();
}
return x * f;
}
int main() {
int n = 0, m = read(), t = read(), ans = 0;
for (int i = 1; i <= m; ++i) {
int x = read();
if (!n && x <= 0) continue;
if (!n || x * a[n] < 0) a[++n] = x;
else a[n] += x;
}
if (a[n] <= 0) --n;
m = t;
for (int i = 1; i <= n; ++i) {
if (i & 1) ans += a[i];
else a[i] = -a[i];
Q.push((Node){a[i], i});
L[i] = i - 1, R[i] = i + 1;
}
a[0] = a[n+1] = a[n+2] = inf, L[0] = R[n+1] = n + 2;
n = (n + 1) / 2;
for (int i = 1; i <= n - m; ++i) {
Node now = Q.top();
while (vis[now.y]) Q.pop(), now = Q.top();
Q.pop(), ans -= now.x;
int l = L[now.y], r = R[now.y];
vis[l] = vis[r] = true;
L[now.y] = L[l], R[now.y] = R[r], L[R[now.y]] = R[L[now.y]] = now.y;
a[now.y] = a[l] + a[r] - a[now.y];
Q.push((Node){a[now.y], now.y});
}
printf("%d\n", ans);
return 0;
}
「POJ Challenge」生日礼物的更多相关文章
- 【链表】BZOJ 2288: 【POJ Challenge】生日礼物
2288: [POJ Challenge]生日礼物 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 382 Solved: 111[Submit][S ...
- BZOJ2288: 【POJ Challenge】生日礼物
2288: [POJ Challenge]生日礼物 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 284 Solved: 82[Submit][St ...
- bzoj 2288 【POJ Challenge】生日礼物 双向链表+堆优化
2288: [POJ Challenge]生日礼物 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1003 Solved: 317[Submit][ ...
- 2288: 【POJ Challenge】生日礼物
2288: [POJ Challenge]生日礼物 https://lydsy.com/JudgeOnline/problem.php?id=2288 分析: 贪心+堆+链表. 首先把序列变一下,把相 ...
- 【BZOJ 2288】 2288: 【POJ Challenge】生日礼物 (贪心+优先队列+双向链表)
2288: [POJ Challenge]生日礼物 Description ftiasch 18岁生日的时候,lqp18_31给她看了一个神奇的序列 A1, A2, ..., AN. 她被允许选择不超 ...
- [bzoj2288]【POJ Challenge】生日礼物_贪心_堆
[POJ Challenge]生日礼物 题目大意:给定一个长度为$n$的序列,允许选择不超过$m$个连续的部分,求元素之和的最大值. 数据范围:$1\le n, m\le 10^5$. 题解: 显然的 ...
- bzoj2288【POJ Challenge】生日礼物*
bzoj2288[POJ Challenge]生日礼物 题意: 给一个序列,求不超过m个连续的部分,使元素和最大.序列大小≤100000 题解: 先把连续的正数和负数合并起来,接着如果正数个数小于m则 ...
- 2288.【POJ Challenge】生日礼物 链表+堆+贪心
BZOJ2288 [POJ Challenge]生日礼物 题意: 给一个长度为\(n\)的数组,最多可以选\(m\)个连续段,问选取的最大值是多少 题解: 先把连续的符号相同的值合并,头和尾的负数去掉 ...
- 「POJ 3666」Making the Grade 题解(两种做法)
0前言 感谢yxy童鞋的dp及暴力做法! 1 算法标签 优先队列.dp动态规划+滚动数组优化 2 题目难度 提高/提高+ CF rating:2300 3 题面 「POJ 3666」Making th ...
随机推荐
- SpringSecurity认证流程
SpringSecurity配置 SecurityConfig.java @Override protected void configure(HttpSecurity http) throws Ex ...
- 【python接口自动化】- PyMySQL数据连接
什么是 PyMySQL? PyMySQL是在Python3.x版本中用于连接MySQL服务器的一个库,Python2中则使用mysqldb.它是一个遵循 Python数据库APIv2.0规范, ...
- zzuli-2266 number
题目描述 某人刚学习了数位DP,他在某天忽然思考如下问题: 给定n,问有多少数对<x, y>满足: x, y∈[1, n], x < y x, y中出现的[0, 9]的数码种类相同 ...
- 关于Matlab GUI的一些经验总结
注:此文来自转载,侵删去年做了一些关于Matlab GUI的程序,现在又要做相关的东西,回想起来,当时很多经验没有记录下来,现在回顾起来始终觉得不爽,所以从现在开始,一定要勤写记录. 从简单的例子说起 ...
- 一个汉字在php中占几个字节?
问:一个汉字在php中占几个字节? 答: 对于中文字符,若使用 GBK.Big-5 版本,每个中文字符相当于 2 个字节: 若使用 UTF-8 版本,每个中文字符相当于 3 个字节.
- flex 布局占位符
flex 布局占位符 空 span bug .popover-custom-class .system-guide-container .buttons-box { display: flex; fl ...
- how to updating Node.js and npm
how to updating Node.js and npm 1 Installing Node.js and updating npm How do I update Node.js ? Not ...
- js data type checker
js data type checker js 数据类型检测 "use strict"; /** * * @author xgqfrms * @license MIT * @cop ...
- Array.fill & array padding
Array.fill & array padding arr.fill(value[, start[, end]]) https://developer.mozilla.org/en-US/d ...
- W3C & 弹幕
W3C & 弹幕 弹幕用例规范 Draft Community Group Report 21 August 2020 refs https://w3c.github.io/danmaku/u ...