BZOJ 2288 贪心 +链表
思路:
放个题解好吧.
http://www.cnblogs.com/zyfzyf/p/4114774.html
//By SiriusRen
#include <queue>
#include <cstdio>
#include <algorithm>
using namespace std;
const int N=;
int a[N],s[N],top,n,m,add,minu,tot,next[N],pre[N],vis[N];
struct Node{int v,id;Node(){}Node(int x,int y){v=x,id=y;}};
bool operator<(Node a,Node b){return a.v>b.v;}
priority_queue<Node>pq;
long long ans;
signed main(){
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++){
scanf("%d",&a[i]);
if(a[i]>){
add+=a[i];
if(minu&&top)s[++top]=minu;
minu=;
}
else if(a[i]<){
minu+=a[i];
if(add)s[++top]=add;
add=;
}
}
if(add)s[++top]=add;
for(int i=;i<=top;i++){
if(s[i]>)pq.push(Node(s[i],i)),tot++,ans+=s[i];
else pq.push(Node(-s[i],i)),s[i]=-s[i];
next[i]=i+,pre[i]=i-;
}next[top]=;
if(tot<=m){
printf("%d\n",ans);
return ;
}
else{
for(int i=;i<=tot-m;i++){
Node tp=pq.top();pq.pop();
while(vis[tp.id]&&!pq.empty())
tp=pq.top(),pq.pop();
if(vis[tp.id])break;
ans-=tp.v;
if(pq.empty())break;
int x=tp.id;
if(!pre[x]){
vis[next[x]]=vis[x]=;pre[next[next[x]]]=;
}
else if(!next[x]){
vis[x]=vis[pre[x]]=;next[pre[pre[x]]]=;
}
else{
vis[pre[x]]=vis[next[x]]=;
tp.v=s[x]=s[pre[x]]+s[next[x]]-s[x];
if(next[next[x]])pre[next[next[x]]]=x;
if(pre[pre[x]])next[pre[pre[x]]]=x;
pre[x]=pre[pre[x]];next[x]=next[next[x]];
pq.push(tp);
}
}
printf("%d\n",ans);
}
}
//By SiriusRen#include <queue>#include <cstdio>#include <algorithm>using namespace std;const int N=100050;int a[N],s[N],top,n,m,add,minu,tot,next[N],pre[N],vis[N];struct Node{int v,id;Node(){}Node(int x,int y){v=x,id=y;}};bool operator<(Node a,Node b){return a.v>b.v;}priority_queue<Node>pq;long long ans;signed main(){ scanf("%d%d",&n,&m); for(int i=1;i<=n;i++){ scanf("%d",&a[i]); if(a[i]>0){ add+=a[i]; if(minu&&top)s[++top]=minu; minu=0; } else if(a[i]<0){ minu+=a[i]; if(add)s[++top]=add; add=0; } } if(add)s[++top]=add; for(int i=1;i<=top;i++){ if(s[i]>0)pq.push(Node(s[i],i)),tot++,ans+=s[i]; else pq.push(Node(-s[i],i)),s[i]=-s[i]; next[i]=i+1,pre[i]=i-1; }next[top]=0; if(tot<=m){ printf("%d\n",ans); return 0; } else{ for(int i=1;i<=tot-m;i++){// for(int j=1;j<=top;j++)printf("%d ",next[j]);puts("");// printf("ans=%d\n",ans); Node tp=pq.top();pq.pop(); while(vis[tp.id]&&!pq.empty()) tp=pq.top(),pq.pop(); if(vis[tp.id])break; ans-=tp.v;// printf("v=%d\n",tp.v); if(pq.empty())break; int x=tp.id; if(!pre[x]){ vis[next[x]]=vis[x]=1;pre[next[next[x]]]=0; } else if(!next[x]){ vis[x]=vis[pre[x]]=1;next[pre[pre[x]]]=0; } else{ vis[pre[x]]=vis[next[x]]=1; tp.v=s[x]=s[pre[x]]+s[next[x]]-s[x]; if(next[next[x]])pre[next[next[x]]]=x; if(pre[pre[x]])next[pre[pre[x]]]=x; pre[x]=pre[pre[x]];next[x]=next[next[x]]; pq.push(tp); }// for(int j=1;j<=top;j++)printf("%d ",next[j]);puts(""); } printf("%d\n",ans); }} |
BZOJ 2288 贪心 +链表的更多相关文章
- BZOJ 2288: 【POJ Challenge】生日礼物 贪心 + 堆 + 链表
好像是模拟费用流 Code: #include <bits/stdc++.h> #define setIO(s) freopen(s".in","r" ...
- [BZOJ 1150] [CTSC2007] 数据备份Backup 【贪心 + 链表】
题目链接:BZOJ - 1150 题目分析 可以看出,我们选的 k 条边一定是相邻两点之间的线段.我们可以将每条边看成一个点,那么我们就是要在 n-1 个点中选出互不相邻的 k 个,使它们的和最小. ...
- 【链表】BZOJ 2288: 【POJ Challenge】生日礼物
2288: [POJ Challenge]生日礼物 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 382 Solved: 111[Submit][S ...
- BZOJ 2288 【POJ Challenge】生日礼物(贪心+优先队列)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2288 [题目大意] 给出一列数,求最多取m段连续的数字,使得总和最大 [题解] 首先我 ...
- 【BZOJ 2288】 2288: 【POJ Challenge】生日礼物 (贪心+优先队列+双向链表)
2288: [POJ Challenge]生日礼物 Description ftiasch 18岁生日的时候,lqp18_31给她看了一个神奇的序列 A1, A2, ..., AN. 她被允许选择不超 ...
- BZOJ 2288: 【POJ Challenge】生日礼物 堆&&链表
就是堆+链表,十分像 数据备份 对吧? 把相邻的正数和相邻的负数合并成一整个正数块和负数块,最后只剩一些交替相间的正块与负块了吧? 显然,正块的个数<=m时,全部选走就获得了最大权值,否则我们可 ...
- bzoj 2288: 【POJ Challenge】生日礼物【链表+堆】
参考:http://blog.csdn.net/w_yqts/article/details/76037315 把相同符号的连续数字加起来,合并后ans先贪心的加上所有正数,如果正数个数sum> ...
- A Magic Lamp(贪心+链表)
A Magic Lamp Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...
- bzoj 2288 【POJ Challenge】生日礼物 双向链表+堆优化
2288: [POJ Challenge]生日礼物 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1003 Solved: 317[Submit][ ...
随机推荐
- 迭代器与index遍历
迭代器用于链式组织的序列. index用于线性组织的序列.
- 数据迁移 Migration
- Python【每日一问】35
问: 基础题: 从键盘输入4个数字,各数字采用空格分隔,对应为变量x0,y0,x1,y1.计算(x0,y0)和(x1,y1)两点之间的距离,输出结果保留1位小数. 比如,键盘输入:0 1 3 5,屏幕 ...
- 【Leetcode】【简单】【14最长公共前缀】【JavaScript】
题目 14. 最长公共前缀 编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["flower",& ...
- (C/C++学习)2.C语言中文件流操作基本函数总结
函数所在头文件:stdio.h 说明:前半部分主要为对各个文件流操作函数的例举,后半部分着重于上机运行分析.文中部分引用自王桂林老师的C/C++课件. 1.FIELE *fopen(const cha ...
- [Ynoi2016]谁的梦
题目大意: 给定$n$个序列,要你从每个序列中选一个非空子串然后拼起来,拼成的序列的贡献为不同元素个数. 支持单点修改,在开始时和每次修改完后,输出所有不同选取方案的贡献和. 解题思路: 窝又来切Yn ...
- touch:命令创建文件
touch:创建空文件或改变文件的时间戳属性 [功能说明] touch命令有两个功能:一是创建新的空文件:二是改变已有文件的时间戳属性 [语法格式] touch [option] [file] tou ...
- 爬虫系列(十三) 用selenium爬取京东商品
这篇文章,我们将通过 selenium 模拟用户使用浏览器的行为,爬取京东商品信息,还是先放上最终的效果图: 1.网页分析 (1)初步分析 原本博主打算写一个能够爬取所有商品信息的爬虫,可是在分析过程 ...
- Python学习笔记之异常处理
1.概念 Python 使用异常对象来表示异常状态,并在遇到错误时引发异常.异常对象未被捕获时,程序将终止并显示一条错误信息 >>> 1/0 # Traceback (most re ...
- 63.es中的type数据类型
主要知识点 理解es中的type数据类型 一.type的理解 type是一个index中用来区分类似的数据的,但是可能有不同的fields,而且有不同的属性来控制索引建立.分词器.field的 ...