2288: 【POJ Challenge】生日礼物

https://lydsy.com/JudgeOnline/problem.php?id=2288

分析:

  贪心+堆+链表。

  首先把序列变一下,把相邻的同符号的合并起来,让序列的第一个是整数,最后一个也是整数。

  如果直接算最大的选的不好算,那么考虑算最小的不选的,正难则反

  然后把所有的整数都加起来,这就是最大的共和。如果此时的段数<=m,那么直接输出就好了。否则,需要选几个数字,来合并它左右的段,以此是段的总数-1。

  选的是一个负数,表示,左边的段后右边的段经过这个负数合起来了。选的是一个正数,那么表示这个正数不要了。由于减去的都是绝对值的大小,那么可以把序列变成正的。此时问题转化为,选k个数,使得它们的和最小,并且不能相邻。

  然后用堆+链表维护。类似上一道题。

代码:

 #include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<cmath>
#include<cctype>
#include<set>
#include<queue>
#include<vector>
#include<map>
using namespace std;
typedef long long LL; inline int read() {
int x=,f=;char ch=getchar();for(;!isdigit(ch);ch=getchar())if(ch=='-')f=-;
for(;isdigit(ch);ch=getchar())x=x*+ch-'';return x*f;
} const int N = ;
const int INF = 1e9; struct Node{
int x, id;
Node() {}
Node(int a,int b) { x = a, id = b; }
bool operator < (const Node &A) const {
return x > A.x;
}
};
priority_queue< Node > q;
int pre[N], nxt[N], vis[N], a[N], b[N];
int cnt; void del(int x) {
if (!x || x == cnt + ) return ;
vis[x] = ;
b[x] = INF;
nxt[pre[x]] = nxt[x];
pre[nxt[x]] = pre[x];
} int main() {
int n = read(), m = read(); cnt = ; b[] = a[] = read();
int last = a[];
for (int i = ; i <= n; ++i) {
a[i] = read();
if (a[i] == ) continue;
if (a[i] * last < ) b[++cnt] = a[i];
else b[cnt] += a[i];
last = a[i];
}
if (b[cnt] < ) cnt --;
if (b[] < ) { cnt --; for (int i = ; i <= cnt; ++i) b[i] = b[i + ]; }
int ans = , d = ;
for (int i = ; i <= cnt; ++i) {
if (b[i] > ) ans += b[i], d ++;
else b[i] = -b[i];
q.push(Node(b[i], i));
}
if (d <= m) { cout << ans << " "; return ; }
else d = d - m; b[] = INF, b[cnt + ] = INF;
for (int i = ; i <= cnt + ; ++i) nxt[i] = i + , pre[i] = i - ;
pre[] = ; nxt[cnt + ] = cnt + ; while (d --) {
while (vis[q.top().id]) q.pop();
Node now = q.top(); q.pop();
int i = now.id; ans -= now.x;
int t = min(INF, b[pre[i]] + b[nxt[i]] - now.x);
del(pre[i]), del(nxt[i]);
b[i] = t; q.push(Node(b[i], i));
}
cout << ans;
return ;
}

2288: 【POJ Challenge】生日礼物的更多相关文章

  1. BZOJ2288:[POJ Challenge]生日礼物——题解

    https://www.lydsy.com/JudgeOnline/problem.php?id=2288 ftiasch 18岁生日的时候,lqp18_31给她看了一个神奇的序列 A1, A2, . ...

  2. BZOJ2288:[POJ Challenge]生日礼物

    浅谈堆:https://www.cnblogs.com/AKMer/p/10284629.html 题目传送门:https://lydsy.com/JudgeOnline/problem.php?id ...

  3. [bzoj2288][POJ Challenge]生日礼物

    用堆维护双向链表来贪心... 数据范围显然不容许O(nm)的傻逼dp>_<..而且dp光是状态就n*m个了..显然没法优化 大概就会想到贪心乱搞了吧...一开始想贪心地通过几段小的负数把正 ...

  4. BZOJ3502PA2012Tanie linie&BZOJ2288[POJ Challenge]生日礼物——模拟费用流+链表+堆

    题目描述 n个数字,求不相交的总和最大的最多k个连续子序列. 1<= k<= N<= 1000000. 输入 输出 样例输入 5 2 7 -3 4 -9 5 样例输出 13   根据 ...

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. Kali-linux在树莓派上破解无线网络

    大部分的命令可以正常的运行在BackTrack5或Kali上.在Kali上可以实现的无线渗透测试,在树莓派上也可以运行.在第1章中介绍了在树莓派上安装Kali Linux操作系统,下面将介绍在树莓派上 ...

  2. StackExchange.Redis学习笔记(二) Redis查询 五种数据类型的应用

    ConnectionMultiplexer ConnectionMultiplexer 是StackExchange.Redis的核心对象,用这个类的实例来进行Redis的一系列操作,对于一个整个应用 ...

  3. spring整合mongo及调用

    spring整合mongo(maven工程下): 1.web.xml文件中配置需要加载的配置文件: <listener> <listener-class>org.springf ...

  4. 理解java的三大特性之多态

    所谓多态就是指程序中定义的引用变量所指向的具体类型和通过该引用变量发出的方法调用在编程时并不确定,而是在程序运行期间才确定,即一个引用变量倒底会指向哪个类的实例对象,该引用变量发出的方法调用到底是哪个 ...

  5. .net根据经纬度获取地址(百度api)

    private string GetAddress(string lng, string lat) { try { string url = @"http://api.map.baidu.c ...

  6. Oracle 体系结构五

    确定实例是否是RAC数据库的一部分:select parallel from v$instance; 确定数据库是否通过Data Guard备用数据库的保护来防止数据丢失:select protect ...

  7. Xcode官方xip直接离线下载地址(更新到Xcode 9.4.1)

    Xcode 9.4.1 https://download.developer.apple.com/Developer_Tools/Xcode_9.4.1/Xcode_9.4.1.xip Xcode 9 ...

  8. 初识ExtJS 6----自学笔记(一)

    一.使用环境 这一点写在前面,是为了方便大家在找资料的时候可以直接定位环境版本. ExtJS版本 6.2  中文官方网站提供版本,网站地址http://extjs.org.cn/node/793 开发 ...

  9. BZOJ 4520: [Cqoi2016]K远点对(k-d tree)

    Time Limit: 30 Sec  Memory Limit: 512 MBSubmit: 1162  Solved: 618[Submit][Status][Discuss] Descripti ...

  10. com.alibaba.druid检测排查数据库连接数不释放定位代码

    1.可能标题说的很不明白,其实就是这样一个情况,一个工程项目错误日志出现GetConnectionTimeoutException: wait millis 90000, active 22000的异 ...