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;
}

最新文章

  1. 基于UDP的网络编程
  2. win10如何100%提升网络速度
  3. 滚动轮播效果,.net 没得混看来只能去写js 了
  4. 如何设置iframe高度自适应,在跨域的情况下能做到吗?
  5. iOS 利用constraint实现2个控件上下的空白是相等的
  6. SKViedoNode类
  7. android run process
  8. Java中System.getProperty()的参数
  9. vue2 watch引用类型 失败原因
  10. Java JSON数据处理
  11. hbuilder IOS APP 打包与发布
  12. 在linux环境下搭建JDK+JAVA+Mysql,并完成jforum的安装
  13. LeetCode(75):分类颜色
  14. Java 中 static 和 volatile 关键字的区别?
  15. bzoj1008/luogu3197 越狱 (快速幂)
  16. Eclipse Oxygen创建maven web项目(一)
  17. C语言程序设计I—第四周教学
  18. How to install sharepoint server 2010 sp2 in window 7 x64
  19. 后续博客转移到zhylj.cc
  20. LeetCode——Diameter of Binary Tree

热门文章

  1. 查找命令中grep,find,which和whereis的使用及区别
  2. POJ 1742 Coins 【可行性背包】【非原创】
  3. Leetcode(878)-第 N 个神奇数字
  4. redis键过期时间
  5. hdu5693D++游戏 区间DP-暴力递归
  6. flutter sqlite持久化数据
  7. py 使用win32 api
  8. 聚焦 2021 NGK 新加坡区块链技术峰会,探讨DeFi未来新生态!
  9. 利用 Java 操作 Jenkins API 实现对 Jenkins 的控制详解
  10. uni-app小白入门自学笔记(一)