$n \leq 50000$的序列,问选不超过$m \leq 50000$个区间使得和最大。

如果正数区间总数比$m$小那肯定全选。否则有两种方式减少区间数量:丢掉一个正区间;补一个负区间连接两个正区间。贪心即可。

先把左右端的负数去掉,然后把正区间和负区间处理出来。优先队列维护区间值,然后开个链表模拟合并(删左右,改自己)。注意删右边时调整右端点。

 //#include<iostream>
#include<cstring>
#include<cstdio>
//#include<time.h>
//#include<complex>
//#include<set>
#include<queue>
//#include<vector>
#include<algorithm>
#include<stdlib.h>
using namespace std; #define LL long long
int qread()
{
char c; int s=,f=; while ((c=getchar())<'' || c>'') (c=='-') && (f=-);
do s=s*+c-''; while ((c=getchar())>='' && c<=''); return s*f;
} //Pay attention to '-' , LL and double of qread!!!! int n,m;
#define maxn 50011
#define LL long long LL Abs(LL x) {return x>?x:-x;} int a[maxn]; LL sum[maxn];
int b[maxn],lb,ll[maxn],rr[maxn]; bool vis[maxn];
struct qnode
{
LL v; int id;
bool operator > (const qnode &b) const {return v>b.v;}
};
priority_queue<qnode,vector<qnode>,greater<qnode> > q;
int main()
{
n=qread(); m=qread();
for (int i=;i<=n;i++) a[i]=qread();
{
int L=,R=n; while (a[L]<=) L++; while (a[R]<=) R--;
for (int i=L,j=;i<=R;i++,j++) a[j]=a[i];
n=R-L+; a[n+]=;
}
for (int i=;i<=n;i++) sum[i]=sum[i-]+a[i]; LL ans=;
int cnt=;
{
LL tmp=a[],last=;
for (int i=;i<=n+;i++)
{
if (tmp> && a[i]<=)
{
b[++lb]=last;
q.push((qnode){tmp,lb});
ans+=tmp; cnt++; tmp=a[i]; last=i;
}
else if (tmp> && a[i]>) tmp+=a[i];
else if (tmp<= && a[i]>)
{
b[++lb]=last;
q.push((qnode){-tmp,lb});
tmp=a[i]; last=i;
}
else tmp+=a[i];
}
} for (int i=;i<=lb+;i++) ll[i]=i-,rr[i]=i+; b[lb+]=n+;
if (cnt<=m) {printf("%lld\n",ans); return ;}
while (m<cnt--)
{
while (vis[q.top().id]) q.pop();
ans-=q.top().v; int now=q.top().id; q.pop(); if (ll[now]==)
{
vis[rr[now]]=;
vis[now]=;
int u=rr[now]; ll[rr[now]]=ll[now]; rr[ll[now]]=rr[now];
ll[rr[u]]=ll[u]; rr[ll[u]]=rr[u];
}
else if (rr[now]==lb+)
{
vis[ll[now]]=;
vis[now]=;
int u=ll[now]; ll[rr[now]]=ll[now]; rr[ll[now]]=rr[now];
ll[rr[u]]=ll[u]; rr[ll[u]]=rr[u]; b[lb+]=b[u];
}
else
{
int L=b[ll[now]],R=b[rr[rr[now]]]-;
vis[ll[now]]=; vis[rr[now]]=; int u=ll[now],v=rr[now];
b[now]=L; q.push((qnode){Abs(sum[R]-sum[L-]),now});
ll[rr[u]]=ll[u]; rr[ll[u]]=rr[u];
ll[rr[v]]=ll[v]; rr[ll[v]]=rr[v];
}
} printf("%lld\n",ans);
return ;
}

最新文章

  1. React,React Native中的es5和es6写法对照
  2. HTML实践发现(标签&lt;pre&gt;)
  3. cocoa框架 for iOS
  4. CSS3-transform变形功能
  5. BaseFragment
  6. 类库dll引用不成功问题
  7. redis之lua脚本
  8. svn: Working copy &#39;D:\workspace\web\..\..\images&#39; is too old (format 10, created by Subversion 1.6
  9. Web Service进阶(七)浅谈SOAP Webservice和RESTful Webservice
  10. CSS概念,引入,选择器
  11. react-devtool 消息处理渲染 源码理解
  12. Devexpress的DateEdit控件中DateTime与EditValue异同
  13. python—列表生成式
  14. update_or_create()
  15. jQuery 练习:取出数组字典的值, 静态对话框, clone方法应用
  16. 跟厂长学PHP7内核(五):系统分析生命周期
  17. c++ o2 优化
  18. hash(2018年CSUST省赛选拔赛第一场B题+hash+字典树)
  19. python ORM - sqlalchemy 操作使用
  20. Using Request Headers for Metadata Address

热门文章

  1. 什么是Java Marker Interface(标记接口)
  2. JAVA 数据库编程中的性能优化
  3. iOS 随机数(Fixed)
  4. pathForResource获取资源为nil的原因
  5. 机器学习(1)- 概述&amp;线性回归&amp;逻辑回归&amp;正则化
  6. Asp.Net Core 进阶(三)—— IServiceCollection依赖注入容器和使用Autofac替换它
  7. Caused by: java.lang.IllegalStateException: Ambiguous mapping. Cannot map &#39;userController&#39; method
  8. ant design table td 文字显示过长添加省略号、ant 文字过长时添加tootip提示
  9. 变色龙启动MAC时,错误信息“ntfs_fixup: magic doesn&#39;t match:”的解决办法
  10. bzoj5183 [Baltic2016]Park