【题目链接】

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

【算法】

先将这个序列的正负数合并起来,变成一个正负交替的序列

如果新序列的正数个数小于等于M,那么直接输出正数的和即可

否则,我们可以将某些正数和负数合并起来,或者不要某些正数

将所有数按绝对值排序,放入堆中,问题就转化为了 : 在这些数中选出(Cnt - M)个数(其中Cnt为正数的个数),

选了一个数后相邻的两个数就不能选,使得最后的和尽可能小

这个问题可以用CTSC2007数据备份的方法来解决,详见 : https://www.cnblogs.com/evenbao/p/9252503.html

【代码】

#include<bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + ;
const int INF = 2e9; struct info
{
int d,pos;
}; int i,n,m,l,r,cnt,ans,len;
int a[MAXN],b[MAXN],val[MAXN],pre[MAXN],nxt[MAXN];
bool visited[MAXN];
info tmp; class Heap
{
private :
int tot;
info hp[MAXN];
public :
inline bool cmp(info a,info b)
{
return a.d < b.d;
}
inline void Up(int x)
{
if (x == ) return;
int fa = x >> ;
if (cmp(hp[x],hp[fa]))
{
swap(hp[x],hp[fa]);
Up(fa);
}
}
inline void Down(int x)
{
int son = x << ;
if (son > tot) return;
if ((son + <= tot) && cmp(hp[son+],hp[son])) son++;
if (cmp(hp[son],hp[x]))
{
swap(hp[son],hp[x]);
Down(son);
}
}
inline void insert(info x)
{
tot++;
hp[tot] = x;
Up(tot);
}
inline void del()
{
swap(hp[],hp[tot]);
tot--;
Down();
}
inline info get()
{
return hp[];
}
} H; int main()
{ scanf("%d%d",&n,&m);
for (i = ; i <= n; i++) scanf("%d",&a[i]);
while (n && a[n] <= ) n--;
i = ;
while (i <= n && a[i] <= ) i++;
if (i > n)
{
printf("%d\n",);
return ;
}
for (; i <= n; i++)
{
if ((a[i] > && a[i-] > ) || (a[i] <= && a[i-] <= )) b[len] += a[i];
else b[++len] = a[i];
}
for (i = ; i <= len; i++)
{
if (b[i] > )
{
ans += b[i];
cnt++;
} else b[i] = -b[i];
}
if (cnt <= m)
{
printf("%d\n",ans);
return ;
}
for (i = ; i <= len; i++)
{
pre[i] = i - ;
nxt[i] = i + ;
H.insert((info){b[i],i});
}
b[] = b[len+] = INF;
for (i = m; i < cnt; i++)
{
tmp = H.get();
while (visited[tmp.pos])
{
H.del();
tmp = H.get();
}
ans -= tmp.d;
H.del();
visited[pre[tmp.pos]] = true;
visited[nxt[tmp.pos]] = true;
b[tmp.pos] = b[pre[tmp.pos]] + b[nxt[tmp.pos]] - tmp.d;
nxt[pre[pre[tmp.pos]]] = tmp.pos;
pre[tmp.pos] = pre[pre[tmp.pos]];
pre[nxt[nxt[tmp.pos]]] = tmp.pos;
nxt[tmp.pos] = nxt[nxt[tmp.pos]];
H.insert((info){b[tmp.pos],tmp.pos});
}
printf("%d\n",ans); return ; }

【BZOJ 2288】 生日礼物的更多相关文章

  1. Bzoj 2288 生日礼物题解

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

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

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

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

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

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

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

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

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2288 [题目大意] 给出一列数,求最多取m段连续的数字,使得总和最大 [题解] 首先我 ...

  6. BZOJ 2288: 【POJ Challenge】生日礼物 堆&&链表

    就是堆+链表,十分像 数据备份 对吧? 把相邻的正数和相邻的负数合并成一整个正数块和负数块,最后只剩一些交替相间的正块与负块了吧? 显然,正块的个数<=m时,全部选走就获得了最大权值,否则我们可 ...

  7. bzoj 2288: 【POJ Challenge】生日礼物【链表+堆】

    参考:http://blog.csdn.net/w_yqts/article/details/76037315 把相同符号的连续数字加起来,合并后ans先贪心的加上所有正数,如果正数个数sum> ...

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

    好像是模拟费用流 Code: #include <bits/stdc++.h> #define setIO(s) freopen(s".in","r" ...

  9. [BZOJ 1293] 生日礼物

    Link: BZOJ 1293 传送门 Solution: 这题直接上尺取法就行了吧 先将每种颜色第一个放入优先队列,用$mx$维护当前的末尾位置 每次取出第一个颜色,更新答案.将其下一个放入队列中去 ...

随机推荐

  1. JS——client

    clientTop.clientLeft: clientTop:盒子的上boder clientLeft:盒子的左border clientWidth与clientHeight 1.在有DTD情况下: ...

  2. JS——缓动动画

    核心思想: (1)相对于匀速移动,盒子每次移动的步长都是变化的,公式:盒子位置=盒子本身位置+(目标位置-盒子本身位置)/10 (2)在盒子位置与目标距离小于10px时,其步长必然是小数,又由于off ...

  3. asp.net MVC 下拉多级联动及编辑

    多级联动实现,附源码.当前,部分代码是参与博客园其它网友. 新增,前台代码: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 2 ...

  4. Java_Web三大框架之Hibernate+jsp+HQL分页查询

    分页查询无处不在.使用Hibernate+jsp+HQL进行分页查询. 第一步:编写房屋实体类和House.hbm.xml映射. /* * 房屋实体类 */ public class House { ...

  5. PHP7安装Memcache+Memcached缓存加速WordPress教程

    PHP7安装Memcache+Memcached缓存加速WordPress教程 2016年1月19日 6,691 Views 生活方式 PHP7最显著的变化就是性能的极大提升,已接近Facebook开 ...

  6. Python3:numpy模块中的argsort()函数

    Python3:numpy模块中的argsort()函数   argsort函数是Numpy模块中的函数: >>> import numpy >>> help(nu ...

  7. SWING界面

    import java.awt.FlowLayout;import javax.swing.*;import java.awt.Container; public class kk extends J ...

  8. STL源码分析之内存池

    前言 上一节只分析了第二级配置器是由多个链表来存放相同内存大小, 当没有空间的时候就向内存池索取就行了, 却没有具体分析内存池是怎么保存空间的, 是不是内存池真的有用不完的内存, 本节我们就具体来分析 ...

  9. 5.terms搜索多个值以及多值搜索结果优化

    主要知识点 terms搜索多个值,并和term的比较     一.term和terms terms是在这个字段中搜索多个值,相当于sql中的in语法 (select * from tbl where ...

  10. How to start a pdf reader from a Linux command line?

    Before you do this, you should be in a GOME or KDE environment, then type the following commands to ...