【BZOJ 2288】 2288: 【POJ Challenge】生日礼物 (贪心+优先队列+双向链表)
2288: 【POJ Challenge】生日礼物
Description
ftiasch 18岁生日的时候,lqp18_31给她看了一个神奇的序列 A1, A2, ..., AN. 她被允许选择不超过 M 个连续的部分作为自己的生日礼物。
自然地,ftiasch想要知道选择元素之和的最大值。你能帮助她吗?
Input
第1行,两个整数 N (1 ≤ N ≤ 105) 和 M (0 ≤ M ≤ 105), 序列的长度和可以选择的部分。
第2行, N 个整数 A1, A2, ..., AN (0 ≤ |Ai| ≤ 104), 序列。
Output
一个整数,最大的和。
Sample Input
5 2
2 -3 2 -1 2Sample Output
5HINT
Source
【分析】
我好笨啊。。。
首先可以把序列弄的好看点,0删掉,负数一段合并,正数一段合并。
那么就是- + - + - 交替。
假设我们先把所有正段选了。假设选了cnt段。
如果cnt<=m,那么这显然就是答案。
否则,按照他们的绝对值扔进小根堆里面,每次选队顶元素。ans-=他的值。
如果选到正数,表示把这个正数删掉,就少了一段。
如果选到负数,表示把负数两边的两段连起来,也少了一段。
知道这个意思之后就知道两端的负数是不可以选的,因为没有意义。
然后删掉那个数之后和两边的两段合并起来重新扔到堆里面做。。【里面包含后悔操作!!!!思考一下!!!!
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
#define Maxn 100100 struct node
{
int id,x;
friend bool operator < (node x,node y)
{
return x.x>y.x;
}
}; priority_queue<node > q; int a[Maxn],w[*Maxn];
int lt[*Maxn],nt[*Maxn];
bool mark[*Maxn]; int myabs(int x) {return x<?-x:x;} int main()
{
int n,m;
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++) scanf("%d",&a[i]);
w[]=a[];
int p=;
for(int i=;i<=n;i++)
{
if(a[i]==) continue;
if((a[i]>=&&w[p]>=)||(a[i]<=&&w[p]<=)) w[p]+=a[i];
else w[++p]=a[i];
}
int cnt=,ans=;
for(int i=;i<=p;i++) if(w[i]>) cnt++,ans+=w[i];
if(cnt>m)
{
m=cnt-m;
memset(mark,,sizeof(mark));
while(!q.empty()) q.pop();
for(int i=;i<=p;i++) nt[i]=i+;nt[p]=-;
for(int i=;i<=p;i++) lt[i]=i-;
for(int i=;i<=p;i++)
{
node xx;
xx.id=i;
xx.x=myabs(w[i]);
q.push(xx);
}
cnt=p;
for(int i=;i<=m;i++)
{
while(mark[q.top().id]) q.pop();
node xx=q.top();q.pop();
if(lt[xx.id]==)
{
lt[nt[xx.id]]=;
if(w[xx.id]<)
{
i--;
continue;
}
ans-=xx.x;
}
else if(nt[xx.id]==-)
{
nt[lt[xx.id]]=-;
if(w[xx.id]<)
{
i--;
continue;
}
ans-=xx.x;
}
else
{
ans-=xx.x;
mark[lt[xx.id]]=mark[nt[xx.id]]=;
cnt++;
nt[lt[lt[xx.id]]]=lt[nt[nt[xx.id]]]=cnt;
lt[cnt]=lt[lt[xx.id]];nt[cnt]=nt[nt[xx.id]];
node nw;
nw.id=cnt;
nw.x=myabs(w[nt[xx.id]]+w[lt[xx.id]]+w[xx.id]);
w[cnt]=w[nt[xx.id]]+w[lt[xx.id]]+w[xx.id];
q.push(nw);
}
}
}
printf("%d\n",ans);
return ;
}
给一个sample in:
5
53 -20 3 -27 68 out:
77
发发表情更健康

2017-01-14 11:01:57
【BZOJ 2288】 2288: 【POJ Challenge】生日礼物 (贪心+优先队列+双向链表)的更多相关文章
- bzoj 2288 【POJ Challenge】生日礼物 双向链表+堆优化
2288: [POJ Challenge]生日礼物 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1003 Solved: 317[Submit][ ...
- 【链表】BZOJ 2288: 【POJ Challenge】生日礼物
2288: [POJ Challenge]生日礼物 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 382 Solved: 111[Submit][S ...
- 2288: 【POJ Challenge】生日礼物
2288: [POJ Challenge]生日礼物 https://lydsy.com/JudgeOnline/problem.php?id=2288 分析: 贪心+堆+链表. 首先把序列变一下,把相 ...
- 2288.【POJ Challenge】生日礼物 链表+堆+贪心
BZOJ2288 [POJ Challenge]生日礼物 题意: 给一个长度为\(n\)的数组,最多可以选\(m\)个连续段,问选取的最大值是多少 题解: 先把连续的符号相同的值合并,头和尾的负数去掉 ...
- bzoj 2295: 【POJ Challenge】我爱你啊
2295: [POJ Challenge]我爱你啊 Time Limit: 1 Sec Memory Limit: 128 MB Description ftiasch是个十分受女生欢迎的同学,所以 ...
- BZOJ 2287: 【POJ Challenge】消失之物( 背包dp )
虽然A掉了但是时间感人啊.... f( x, k ) 表示使用前 x 种填满容量为 k 的背包的方案数, g( x , k ) 表示使用后 x 种填满容量为 k 的背包的方案数. 丢了第 i 个, 要 ...
- BZOJ 2287 【POJ Challenge】消失之物(DP+容斥)
2287: [POJ Challenge]消失之物 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 986 Solved: 572[Submit][S ...
- BZOJ 2288 【POJ Challenge】生日礼物(贪心+优先队列)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2288 [题目大意] 给出一列数,求最多取m段连续的数字,使得总和最大 [题解] 首先我 ...
- BZOJ 2288: 【POJ Challenge】生日礼物 贪心 + 堆 + 链表
好像是模拟费用流 Code: #include <bits/stdc++.h> #define setIO(s) freopen(s".in","r" ...
随机推荐
- bzoj3716/4251 [PA2014]Muzeum
传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=3716 http://www.lydsy.com/JudgeOnline/problem.ph ...
- A Simple Math Problem(矩阵快速幂)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1757 思路:矩阵快速幂模板题,不过因为刚刚入门矩阵快速幂,所以经常把数组f存反,导致本地错误一晚,差点 ...
- 子DIV块中设置margin-top时影响父DIV块位置的解决办法?
解决方法: 1.修改父元素的高度,增加padding-top样式模拟(padding-top:1px:常用) 2.为父元素添加overflow:hidden:样式即可(完美) 3.为父元素或者子元素声 ...
- vue_使用npm搭建vue2.0脚手架开发环境
前言: 在使用vue进行开发时需要搭建vue的运行环境,这里主要是使用淘宝镜像cnpm进行搭建vue的脚手架开发环境.主要是分为mac和window两个版本,两个环境的搭建都是大同小异. mac开发环 ...
- base--AuditObject
//参考base-4.0.2.jarpublic class AuditObject extends HashMap<String, Object> implements TimeRefe ...
- 【DataScience学习笔记】Coursera课程《数据科学家的工具箱》 约翰霍普金斯大学——Week3 Conceptual Issues课堂笔记
Coursera课程<数据科学家的工具箱> 约翰霍普金斯大学 Week3 Conceptual Issues Types of Questions Types of Data Scienc ...
- 64_g5
golang-github-kr-text-devel-0-0.11.git6807e77.f..> 11-Feb-2017 07:48 14250 golang-github-kr-text- ...
- sicily 1059. Exocenter of a Trian
Description Given a triangle ABC, the Extriangles of ABC are constructed as follows: On each side of ...
- SPOJ 375
默默一看提交时间 -- 这是我以前的代码风格-- #include <cstdio> #include <cstring> #include <vector> #i ...
- 当你用element-ui遇到需要在el-table-column上v-for时,这篇文章你能用的上,也就是你需要二级循环
好链接就要丢过去 https://blog.csdn.net/qq_28929589/article/details/79445354
