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][ ...
随机推荐
- centos开机运行级别更改
1.使用命令切换运行级别/目标 # systemctl isolate multi-user.target //切换到运行级别3,该命令对下次启动无影响,等价于telinit 3 # systemct ...
- day36 类的三大特性---封装以及Property特性
目录 类的封装 如果真的要拿 类的property特性 setter & deleter 类属性用法 类与对象的绑定方法和非绑定方法 对象方法&类方法&静态方法 隐藏模块内的函 ...
- lvs负载均衡net模式
环境配置,一台双网卡的ens33,ens37,ens37的网关是ens33的IP,指定一下nginx ens33,192.168.30.22,ens37,172.16.1.1nginx 192.16 ...
- 01-Linux命令基础-第01天(命令基础,软件安装与卸载、磁盘管理)
01- Linux初步 最早一直是单道程序设计模型的操作系统 69年贝尔实验室决定开发多道程序设计模型的操作系统 Multics计划 (失败了) x86 IA(Intel Architecture ...
- Linux 中, 安装html转pdf工具:wkhtmltopdf
wkhtmltopdf下载地址官网:https://wkhtmltopdf.org/downloads.html 进入到/opt文件夹下面,新建文件夹wkhtmltopdf,然后把下载好的wkhtml ...
- Intro.js 分步向导插件使用方法
简介 为您的网站和项目提供一步一步的.更好的介绍 Intro.js 目前兼容 Firefox.Chrome 和 IE8,不兼容 IE6 和 IE7,后续版本将会提供更好的兼容. 在线演示及下载 在线演 ...
- Redis启动出错 noauth authentication required.
输入认证过的密码即可. 在命令行中运行: auth password
- nyoj 547 优先队列
#include<stdio.h> #include<string.h> #include<queue>//水杯盛水问题,用优先队列不断从最小的边缘开始 using ...
- mysql explain介绍
mysql环境优化: 1.如果order by 没有利用到索引,那么将会出现fileSort,如果sort_buffer不够大,fileSort过程则需要使用临时文件 ,fileSort优化,主要通过 ...
- MySQL:浅析 Impossible WHERE noticed after reading const tables
使用 EXPLAIN 执行计划的时候,在 Extra 中偶尔会看到这样的描述: Impossible WHERE noticed after reading const tables 字面上的意思是: ...