就是堆+链表,十分像 数据备份 对吧?

把相邻的正数和相邻的负数合并成一整个正数块和负数块,最后只剩一些交替相间的正块与负块了吧?

显然,正块的个数<=m时,全部选走就获得了最大权值,否则我们可能需要选一些负块来获得最优解。

然而弱不经风的我调了四个小时链表和预处理QAQ。。。

千万不要犯此种错误:

    n=g(),m=g();
for(R i=;i<=n;++i) a[i]=g();
vl[cnt]=a[],pre[]=;
for(R i=;i<=n;++i) if(sgn(a[i])==sgn(a[i-])&&sgn(a[i])) vl[cnt]+=a[i];//,cnt==1?cerr<<"%"<<vl[cnt]<<" ":cerr<<"";
else if(sgn(a[i]))vl[++cnt]=a[i],nxt[cnt-]=cnt,pre[cnt]=cnt-; nxt[cnt]=;

上面这种写法会吧中间有0的同号块分成两块。。。。

AC代码:

#include<cstdio>
#include<iostream>
#include<queue>
#define R register int
using namespace std;
const int N=;
int cnt,n,m,pst,neg,tot,ans=;
int a[N],vl[N],pre[N],nxt[N];
bool vis[N];
struct node{
int vl,pos;
node() {}
node(int vvl,int ppos):vl(vvl),pos(ppos){}
bool operator <(const node& y)const {return vl>y.vl;}
};
priority_queue<node> q;
inline int g() {
R ret=,fix=; register char ch; while(!isdigit(ch=getchar())) fix=ch=='-'?-:fix;
do ret=ret*+(ch^); while(isdigit(ch=getchar())); return ret*fix;
}
inline int abs(int x) {return x>?x:-x;}
signed main() {
n=g(),m=g();
for(R i=;i<=n;++i) {
a[i]=g();
if(a[i]>){pst+=a[i]; neg&&cnt?vl[++cnt]=neg,neg=:neg=;}
if(a[i]<){neg+=a[i]; pst?vl[++cnt]=pst,pst=:pst=;}
} pst?vl[++cnt]=pst:pst=;
for(R i=;i<=cnt;++i) {
q.push(node(abs(vl[i]),i));
vl[i]>?++tot,ans+=vl[i]:vl[i]=-vl[i];
nxt[i]=i+,pre[i]=i-;
} nxt[cnt]=;
for(R i=;i+m<=tot;++i) {
register node tmp=q.top();q.pop();
while(vis[tmp.pos]&&!q.empty()) tmp=q.top(),q.pop();
if(vis[tmp.pos]) break;
ans-=tmp.vl; if(q.empty()) break;
R pos=tmp.pos;
if(!pre[pos]) vis[nxt[pos]]=vis[pos]=true,pre[nxt[nxt[pos]]]=;
else if(!nxt[pos]) vis[pre[pos]]=vis[pos]=true,nxt[pre[pre[pos]]]=;
else {
vis[pre[pos]]=vis[nxt[pos]]=true;
tmp.vl=vl[pos]=vl[pre[pos]]+vl[nxt[pos]]-vl[pos];
if(nxt[nxt[pos]]) pre[nxt[nxt[pos]]]=pos;
if(pre[pre[pos]]) nxt[pre[pre[pos]]]=pos;
pre[pos]=pre[pre[pos]],nxt[pos]=nxt[nxt[pos]];
q.push(tmp);
}
} printf("%d\n",ans);
}

2019.04.06

BZOJ 2288: 【POJ Challenge】生日礼物 堆&&链表的更多相关文章

  1. 【BZOJ3502/2288】PA2012 Tanie linie/【POJ Challenge】生日礼物 堆+链表(模拟费用流)

    [BZOJ3502]PA2012 Tanie linie Description n个数字,求不相交的总和最大的最多k个连续子序列. 1<= k<= N<= 1000000. Sam ...

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

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

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

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

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

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

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

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

  6. BZOJ.2287.[POJ Challenge]消失之物(退背包)

    BZOJ 洛谷 退背包.和原DP的递推一样,再减去一次递推就行了. f[i][j] = f[i-1][j-w[i]] + f[i-1][j] f[i-1][j] = f[i][j] - f[i-1][ ...

  7. BZOJ 2295: [POJ Challenge]我爱你啊

    由于是子序列,那么难度就在于读入 #include<cstdio> #include<algorithm> #include<cstring> using name ...

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

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

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

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

随机推荐

  1. iOS实时监控网络状态的改变

    在网络应用中,有的时候需要对用户设备的网络状态进行实时监控,有两个目的: (1)让用户了解自己的网络状态,防止一些误会(比如怪应用无能) (2)根据用户的网络状态进行智能处理,节省用户流量,提高用户体 ...

  2. 分享知识-快乐自己:搭建第一个 Hibernate (Demo)

    使用 Hibernate 完成持久化操作 七大 步骤: 1.读取并解析配置文件及映射文件: Configuration configuration=new Configuration().config ...

  3. EmbarassedBirds全体开发人员落泪

    Github (李昆乘,赖展飞) 现阶段还在开发后期,API调试过程中. 本周无法上线. 全体开发人员留下眼泪. 贴上几个功能图, 给大家尝尝鲜吧! 现阶段仍在API调试 因为队员李昆乘经常出去玩没有 ...

  4. java对象的初始化过程和创建对象的几种方式

    1.加载父类,加载父类的静态属性和静态代码块 2.加载子类,加载子类的静态属性和静态代码块 3.初始化父类中的非静态属性并赋初值,执行父类非静态代码块,执行父类构造. 4.初始化子类中的非静态属性并赋 ...

  5. ACM学习历程——UVA442 Matrix Chain Multiplication(栈)

    Description   Matrix Chain Multiplication  Matrix Chain Multiplication  Suppose you have to evaluate ...

  6. python反复执行某个命令

    #! /usr/bin/env python #coding=utf-8 # 以需要的时间间隔执行某个命令    import time, os    def re_exe(cmd, inc = 60 ...

  7. 洛谷P3373线段树模板2

    题目:https://www.luogu.org/problemnew/show/P3373 带乘的线段树,更新时把加的标记也乘一下,然后取值时先乘后加. 代码如下: #include<iost ...

  8. SQLServer 微软团队开源项目 (web 版?)

    http://www.codeplex.com/site/users/view/SQLTeamAdmin

  9. js中全局变量的一点小知识点

    js中有三种方式定义全局变量: 在任何函数外面直接执行var语句,例如:var f="value"; 直接添加一个属性到全局变量上,在web浏览器中,全局对象名为window.例如 ...

  10. python斐波拉契数列

    def fib(max): n, a, b = 0, 0, 1 while n < max: print(b) a, b = b, a + b n = n + 1 return 'done' 注 ...