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

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

显然,正块的个数<=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. hdu 6103(Kirinriki)

    题目链接:Kirinriki 题目描述: 找两个不重叠的字符串A,B. 使得dis(A,B)<=m;\(dis(A,B)= \sum _{i=0}^{n-1} \left | A_i-B_{n- ...

  2. linux ssh 命令使用解析

    前一阵远程维护Linux服务器,使用的是SSH,传说中的secure shell. 登陆:ssh [hostname] -u user 输入密码:***** 登 陆以后就可以像控制自己的机器一样控制它 ...

  3. python程序打包工具 ── cx_Freeze

    cx_Freeze是一个类似py2exe的工具,它们区别是py2exe是将python程序打包成windows下可以执行的exe文件的,而cx_Freeze则是将python程序打包为linux下可以 ...

  4. noip模拟赛 #2

    万年rk2 我写挂大家都挂但是有人比我挂的少 我好不容易卡一波常数然后有人ak ... T1.不想写,等会放链接 T2 给一个方阵,每个地方有一个权值,把它划成两块,不能往回拐弯,求两块极差较大的那个 ...

  5. CodeForces - 613D:Kingdom and its Cities(虚树+DP)

    Meanwhile, the kingdom of K is getting ready for the marriage of the King's daughter. However, in or ...

  6. 关于Socket 多线程 的一篇好文章

    http://www.kegel.com/c10k.html#topIt's time for web servers to handle ten thousand clients simultane ...

  7. ffmpeg 合并文件

    ffmpeg文件合并   文件1.ts~ 100.ts 流媒体文件1.txt 格式 file .ts file .ts ... file .ts 命 令   (-vcodec copy -acodec ...

  8. python list列表sort、sorted、reverse排序

    列表排序:sort是修改原列表,sorted提供原列表的一个有序副本 li=[2,1,4,5,0]li.sort() #默认从小到大print li结果:[0, 1, 2, 4, 5] li=[2,1 ...

  9. 【238】◀▶IEW-Unit03

    Unit 3 Media 柱状图 1.图片及model1分析 The graph below contains information about the average science test s ...

  10. Weblogic学习

    1.WebLogic目录介绍:     coherence:集群组件 modules:第三方的模块包 user_projects:存放域的文件夹(必须要创建域) utils 工具包 uninstall ...