思路:

放个题解好吧.

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 贪心 +链表的更多相关文章

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

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

  2. [BZOJ 1150] [CTSC2007] 数据备份Backup 【贪心 + 链表】

    题目链接:BZOJ - 1150 题目分析 可以看出,我们选的 k 条边一定是相邻两点之间的线段.我们可以将每条边看成一个点,那么我们就是要在 n-1 个点中选出互不相邻的 k 个,使它们的和最小. ...

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

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

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

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

  5. 【BZOJ 2288】 2288: 【POJ Challenge】生日礼物 (贪心+优先队列+双向链表)

    2288: [POJ Challenge]生日礼物 Description ftiasch 18岁生日的时候,lqp18_31给她看了一个神奇的序列 A1, A2, ..., AN. 她被允许选择不超 ...

  6. BZOJ 2288: 【POJ Challenge】生日礼物 堆&&链表

    就是堆+链表,十分像 数据备份 对吧? 把相邻的正数和相邻的负数合并成一整个正数块和负数块,最后只剩一些交替相间的正块与负块了吧? 显然,正块的个数<=m时,全部选走就获得了最大权值,否则我们可 ...

  7. bzoj 2288: 【POJ Challenge】生日礼物【链表+堆】

    参考:http://blog.csdn.net/w_yqts/article/details/76037315 把相同符号的连续数字加起来,合并后ans先贪心的加上所有正数,如果正数个数sum> ...

  8. A Magic Lamp(贪心+链表)

    A Magic Lamp Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

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

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

随机推荐

  1. Java基础学习笔记之:System类;Math类;Arrays类BigInteger,BigDecimal

    System类 在API中System类介绍的比较简单,我们给出定义,System中代表程序所在系统,提供了对应的一些系统属性信息,和系统操作.System类不能手动创建对象,因为构造方法被priva ...

  2. hadoop spark 总结

    yarn  由,资源管理器rm,应用管理器am appMaster,节点管理器nm 组成! 图侵删 yarn 的设计,是为了代替hadoop 1.x的jobtracker 集中式一对多的资源管理「资源 ...

  3. [luogu2624 HNOI2008]明明的烦恼 (prufer+高精)

    传送门 Solution 根据prufer序列做的题,具体可以看这里 还知道了一种避免高精除的方法quq Code #include <cmath> #include <cstdio ...

  4. [poj1325] Machine Schedule (二分图最小点覆盖)

    传送门 Description As we all know, machine scheduling is a very classical problem in computer science a ...

  5. [SDFZOJ]1069:树上统计

    神题...std丑的不行. 我们可以发现i->i+1的边被覆盖过i×(n-i)次. 因为以1->i为左端点,以i+1->n的为右端点,i->i+1都将被覆盖这么多次. 然后从1 ...

  6. tornado服务器运行django应用

    在jumpserver项目中看到的 def main(): from django.core.wsgi import get_wsgi_application import tornado.wsgi ...

  7. Codeforces 816C/815A - Karen and Game

    传送门:http://codeforces.com/contest/816/problem/C 本题是一个模拟问题. 有一个n×m的矩阵.最初,这个矩阵为零矩阵O.现有以下操作: a.行操作“row  ...

  8. 2、Linux的关机方式

    shutdown 关机指令,你可以man shutdown 来看一下帮助文档.例如你可以运行如下命令关机: shutdown –h 10 ‘This server will shutdown afte ...

  9. springcloud 中文文档

    spring cloud 中文文档:https://springcloud.cc/spring-cloud-dalston.html spring cloud 中文网:https://springcl ...

  10. Bi-shoe and Phi-shoe 欧拉函数 素数

    Bamboo Pole-vault is a massively popular sport in Xzhiland. And Master Phi-shoe is a very popular co ...