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」生日礼物的更多相关文章

  1. 【链表】BZOJ 2288: 【POJ Challenge】生日礼物

    2288: [POJ Challenge]生日礼物 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 382  Solved: 111[Submit][S ...

  2. BZOJ2288: 【POJ Challenge】生日礼物

    2288: [POJ Challenge]生日礼物 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 284  Solved: 82[Submit][St ...

  3. bzoj 2288 【POJ Challenge】生日礼物 双向链表+堆优化

    2288: [POJ Challenge]生日礼物 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 1003  Solved: 317[Submit][ ...

  4. 2288: 【POJ Challenge】生日礼物

    2288: [POJ Challenge]生日礼物 https://lydsy.com/JudgeOnline/problem.php?id=2288 分析: 贪心+堆+链表. 首先把序列变一下,把相 ...

  5. 【BZOJ 2288】 2288: 【POJ Challenge】生日礼物 (贪心+优先队列+双向链表)

    2288: [POJ Challenge]生日礼物 Description ftiasch 18岁生日的时候,lqp18_31给她看了一个神奇的序列 A1, A2, ..., AN. 她被允许选择不超 ...

  6. [bzoj2288]【POJ Challenge】生日礼物_贪心_堆

    [POJ Challenge]生日礼物 题目大意:给定一个长度为$n$的序列,允许选择不超过$m$个连续的部分,求元素之和的最大值. 数据范围:$1\le n, m\le 10^5$. 题解: 显然的 ...

  7. bzoj2288【POJ Challenge】生日礼物*

    bzoj2288[POJ Challenge]生日礼物 题意: 给一个序列,求不超过m个连续的部分,使元素和最大.序列大小≤100000 题解: 先把连续的正数和负数合并起来,接着如果正数个数小于m则 ...

  8. 2288.【POJ Challenge】生日礼物 链表+堆+贪心

    BZOJ2288 [POJ Challenge]生日礼物 题意: 给一个长度为\(n\)的数组,最多可以选\(m\)个连续段,问选取的最大值是多少 题解: 先把连续的符号相同的值合并,头和尾的负数去掉 ...

  9. 「POJ 3666」Making the Grade 题解(两种做法)

    0前言 感谢yxy童鞋的dp及暴力做法! 1 算法标签 优先队列.dp动态规划+滚动数组优化 2 题目难度 提高/提高+ CF rating:2300 3 题面 「POJ 3666」Making th ...

随机推荐

  1. LINUX - vim高效操作

    (一)可以为操作的一行添加下划线 set cursorline

  2. Kubernets二进制安装(9)之部署主控节点控制器controller-manager

    kube-controller-manager运行控制器,它们是处理集群中常规任务的后台线程 Controller Manager就是集群内部的管理控制中心,由负责不同资源的多个Controller构 ...

  3. 操作系统 part2

    一.程序的内存结构 references: newcoder 运行时,程序分为:text段.data段.BSS段(2个合称数据段).堆.栈. text段:代码段,静态分配内存,只读. data段:初始 ...

  4. docker安装CentOS7及JNI使用相关过程记录

    docker pull centos:centos7(拉取镜像) docker run -itd --name centos-test centos:centos7 (运行容器) docker exe ...

  5. 2020 Web 全栈面经

    2020 Web 全栈面经 1.简历 2. 技术 3. 项目 4. 架构 5. 沟通,协作 6.成长 7. 面试技巧 准备 电话确认,面试流程,五险一金缴纳情况 有无笔试,几轮,面试时间,答复时间 细 ...

  6. OTA development

    OTA development OTA update OTA Over the Air / 无线下载 https://en.wikipedia.org/wiki/Over-the-air_progra ...

  7. bash for mac

    bash for mac https://sourabhbajaj.com/mac-setup/iTerm/ https://sourabhbajaj.com/mac-setup/iTerm/zsh. ...

  8. how to delete the virtual dom that created in memory using js

    how to delete the virtual dom that created in memory using js const virtualDomConvert = (filename = ...

  9. Flutter: IntrinsicWidth类,将孩子的宽度调整为孩子的最大内在宽度

    文档 原文 class HomePage extends StatefulWidget { @override _HomePageState createState() => _HomePage ...

  10. betterscroll上拉加载

    mounted(){ var that=this; setTimeout(()=>{ let scroll = new BScroll(document.getElementById('wrap ...