「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 ...
随机推荐
- 【转】Dockerfile
1. 关于docker build docker build可以基于Dockerfile和context打包出一个镜像,其中context是一系列在PATH或URL中指定的位置中的文件(contex ...
- 字节笔试题 leetcode 69. x 的平方根
更多精彩文章请关注公众号:TanLiuYi00 题目 解题思路 题目要求非负整数 x 的平方根,相当于求函数 y = √x 中 y 的值. 函数 y = √x 图像如下: 从上图中,可以看出函数是单 ...
- POJ 3415 Common Substrings(后缀数组 + 单调栈)题解
题意: 给两个串\(A.B\),问你长度\(>=k\)的有几对公共子串 思路: 先想一个朴素算法: 把\(B\)接在\(A\)后面,然后去跑后缀数组,得到\(height\)数组,那么直接\(r ...
- HDU 3966 Aragorn's Story(树链剖分)题解
题意:给一棵树,要求你对一个路径上的值进行加减,查询某个点的值 思路:重链剖分. 由于分了轻重儿子,我每次到重儿子的top只要O(1),经过的轻儿子最多logn条,那么我每次往上跳最多跳logn次. ...
- 在竞赛中使用new的问题
问了一下KingSann大佬,大佬说 找空闲内存均摊O(1)但是如果new多了就是O(n) 真tm可怕..还是开个内存池好了.. 要么直接now++,要么直接Node *s=&node[t ...
- Cortex-M系列内核 启动文件分析
最近终于闲了下来了准备好好学习下Cortex-M3/M4系列处理器的架构,经过各种资料的折磨也没法对它的整个工作过程能有个完整的认知,最后看到一片博客打算从程序的运行过程开始探究,所以首先就找到了启动 ...
- vue 二级子路由跳转不了 bug
vue 二级子路由跳转不了 bug @click.prevent 阻止原生事件的冒泡 <li class="tools-hover-box-list-item" v-for= ...
- input composition event All In One
input composition event All In One input event compositionStart & compositionEnd & compositi ...
- open an iOS simulator from the terminal
open an iOS simulator from the terminal # simulator $ open -a Simulator flutter https://flutter.dev/ ...
- lua windows上使用vs编译Lua
video 下载lua源文件 还有种方法: 创建空项目,取名lua,导入lua源文件 将luac.c移除,然后编译lua.exe 将lua.c移除,添加luac.c然后编译lua.exe后重命名位lu ...