2288: 【POJ Challenge】生日礼物

Description

ftiasch 18岁生日的时候,lqp18_31给她看了一个神奇的序列 A1A2, ..., AN. 她被允许选择不超过 M 个连续的部分作为自己的生日礼物。

自然地,ftiasch想要知道选择元素之和的最大值。你能帮助她吗?

Input

第1行,两个整数 N (1 ≤ N ≤ 105) 和 M (0 ≤ M ≤ 105), 序列的长度和可以选择的部分。

第2行, N 个整数 A1A2, ..., AN (0 ≤ |Ai| ≤ 104), 序列。

Output

一个整数,最大的和。

Sample Input

5 2
2 -3 2 -1 2

Sample Output

5

HINT

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】生日礼物 (贪心+优先队列+双向链表)的更多相关文章

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

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

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

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

  3. 2288: 【POJ Challenge】生日礼物

    2288: [POJ Challenge]生日礼物 https://lydsy.com/JudgeOnline/problem.php?id=2288 分析: 贪心+堆+链表. 首先把序列变一下,把相 ...

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

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

  5. bzoj 2295: 【POJ Challenge】我爱你啊

    2295: [POJ Challenge]我爱你啊 Time Limit: 1 Sec  Memory Limit: 128 MB Description ftiasch是个十分受女生欢迎的同学,所以 ...

  6. BZOJ 2287: 【POJ Challenge】消失之物( 背包dp )

    虽然A掉了但是时间感人啊.... f( x, k ) 表示使用前 x 种填满容量为 k 的背包的方案数, g( x , k ) 表示使用后 x 种填满容量为 k 的背包的方案数. 丢了第 i 个, 要 ...

  7. BZOJ 2287 【POJ Challenge】消失之物(DP+容斥)

    2287: [POJ Challenge]消失之物 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 986  Solved: 572[Submit][S ...

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

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

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

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

随机推荐

  1. SpringBoot jar包不支持jsp

    官方原文如下: When running a Spring Boot application that uses an embedded servlet container (and is packa ...

  2. 【BZOJ4869】相逢是问候 [线段树][欧拉定理]

    相逢是问候 Time Limit: 40 Sec  Memory Limit: 512 MB[Submit][Status][Discuss] Description Informatikverbin ...

  3. Sketch VS Photoshop

    参考:http://mp.weixin.qq.com/s?__biz=MjM5NTQ5MjIyMA==&mid=217309554&idx=4&sn=4d6a5239ca813 ...

  4. pandas中DataFrame使用

    切片选择 #显示第一行数据print(df.head(1)) #显示倒数三行数据 print(df.tail(3)) loc  df.loc[row_index,col_index]  注意loc是根 ...

  5. 在linux程序里面,知道一个函数地址,改函数是属于某个动态库的,怎么样得到这个动态库的全【转】

    转自:http://www.360doc.com/content/17/1012/11/48326749_694292472.shtml 另外dl_iterate_phdr可以查到当前进程所装在的所有 ...

  6. centos 安装flash

    linux系统中安装flash插件 linux中安装flashplayer插件的简单方法: 1.下载其中最新版本的播放器,下载地址: http://get.adobe.com/cn/flashplay ...

  7. 己动手创建最精简的Linux

    己动手创建最精简的Linux http://blog.sina.com.cn/s/blog_71c87c170101e7ru.html 首次 LFS 搭建全过程 http://zmyxn.blog.5 ...

  8. perl操作MongoDB

    perl操作MongoDB http://blog.csdn.net/jophyyao/article/details/8223190 Mongodb 的C语言操作 http://blog.csdn. ...

  9. C#使用Linq To XML读取XML,Linq生成XML,Linq创建带属性或带节点XML

    using System; using System.Linq; using System.Xml.Linq; namespace Sample2 { class Program { static v ...

  10. 调用start()与run()的区别

    1.调用start()方法: 通知“线程规划器”当前线程已经准备就绪,等待调用线程对象的run()方法.这个过程就是让系统安排一个时间来调用Thread中的run()方法,使线程得到运行,启动线程,具 ...