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][ ...
随机推荐
- 终极解决VS2015 安装失败问题,如 安装包损坏或丢失
1.去微软官网下载完成ISO镜像,最好不要在线安装, 打开官方链接 https://www.visualstudio.com/zh-cn/downloads/download-visual-studi ...
- 来源页面地址 上一页面url
Uri uri = Request.UrlReferrer;
- (转)C#开发微信门户及应用(2)--微信消息的处理和应答
http://www.cnblogs.com/wuhuacong/p/3614175.html 微信应用如火如荼,很多公司都希望搭上信息快车,这个是一个商机,也是一个技术的方向,因此,有空研究下.学习 ...
- 卸载hyper-v后 仍然提示 vmware 与 hyper-v 不兼容
已经卸载了hyper-v 仍然提示 vmware 与 hyper-v 不兼容:天天模拟器,提示VT模式没有开启,BIOS里面已经设置过了 环境win10,vm的失败和模拟器的失败都是hyper-v冲突 ...
- loadrunner安装方法
1.loadrunner安装网盘地址: http://pan.baidu.com/s/1hrP6mDQ 一般会提示:“vc2005_sp1_with_atl_fix_redist 2.确认时提示缺少 ...
- 爬虫系列(四) 用urllib实现英语翻译
这篇文章我们将以 百度翻译 为例,分析网络请求的过程,然后使用 urllib 编写一个英语翻译的小模块 1.准备工作 首先使用 Chrome 浏览器打开 百度翻译,这里,我们选择 Chrome 浏览器 ...
- keycode键盘 按键 - 键码 对应表
字母和数字键的键码值(keyCode) 按键 键码 按键 键码 按键 键码 按键 键码 A 65 J 74 S 83 1 49 B 66 K 75 T 84 2 50 C 67 L 76 U 85 3 ...
- windows环境下用pip安装pyautogui遇到的几个问题
1.不能直接使用win+r运行cmd并使用pip,必须点击开始->windows系统->命令提示符,右键->以管理员身份运行 2.运行pip install pyautogui后提示 ...
- docker 容器操作( 以 tomcat 为例 )
一.容器操作 一个镜像可以启动多个容器.比如一个 tomcat 镜像,可以启动多个tomcat 容器,启动后的这些 tomcat 都是各自独立的 二.步骤 1.搜索镜像 [root@localhost ...
- i=i+1,i+=1与i++的区别
1. i=i+1 a.读取右i的地址 b,i=1 c.读取左i的地址 d. 值赋给左i 2.i+=1 a.读取左i的地址 b.i+1 c.值给i 3.i++ a.读取右i的地址 b.值加1