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][ ...
随机推荐
- sql 查询替换字符
Select A,B,C from Tb1 –查询所有 Select A,replace(B,’1’,’壹’),C from Tb1 替换查询 Tb1 A B C AA1 1 CC A ...
- LOJ #6041. 「雅礼集训 2017 Day7」事情的相似度 LCT+SAM+线段树
Code: #include<bits/stdc++.h> #define maxn 200003 using namespace std; void setIO(string s) { ...
- 【转载】java的常见类型转换
//Int型数字转换成String int num1=123456; //方法1 String str1=num1+""; System.out.println(str1); // ...
- Python 切片 day3
你可以处理列表的部分元素——Python称之为切片 . 一.使用方法: 要创建切片,可指定要使用的第一个元素和最后一个元素的索引. 与函数range() 一样,Python在到达你指定的第二个索引前面 ...
- 【剑指Offer】22、从上往下打印二叉树
题目描述: 从上往下打印出二叉树的每个节点,同层节点从左至右打印. 解题思路: 本题实际上就是二叉树的层次遍历,深度遍历可以用递归或者栈,而层次遍历很明显应该使用队列.同样我们可以通过 ...
- 分布式系列文章 —— 从 ACID 到 CAP / BASE
转自:https://mp.weixin.qq.com/s?amp;mid=2652037708&__biz=MzI0NDI0MTgyOA%3D%3D&idx=1&chksm= ...
- apache+php连接数据库
######## 安装APACHE ##############安装apr/usr/src/apache+php/tar xf apr-1.5.2.tar.gzcd apr-1.5.2./config ...
- 8.在idea中配置maven
1.在IntelliJ IDEA中配置maven 打开-File-Settings 2.我们还可以在勾选一些其他选项 3.我们可以更新一下本地仓库和远程仓库,这个样在pom.xml文件中添加依赖jia ...
- [bzoj2748][HAOI2012]音量调节_动态规划_背包dp
音量调节 bzoj-2748 HAOI-2012 题目大意:有一个初值,给你n个$\delta$值,求最后不超过给定的限制的情况下的改变的最大值.每个$\delta$值可以+也可以-. 注释:$1\l ...
- 导致“mysql has gone away”的两种情况
导致“mysql has gone away”的两种情况 By Cruise 1. wait_timeout参数 在开发代理server时, 我使用了jdbc连接数据库,并采用长连接的方式连接数据库 ...