51nod1053 最大M子段和 V2
2024-09-08 01:47:59
$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 ;
}
最新文章
- React,React Native中的es5和es6写法对照
- HTML实践发现(标签<;pre>;)
- cocoa框架 for iOS
- CSS3-transform变形功能
- BaseFragment
- 类库dll引用不成功问题
- redis之lua脚本
- svn: Working copy &#39;D:\workspace\web\..\..\images&#39; is too old (format 10, created by Subversion 1.6
- Web Service进阶(七)浅谈SOAP Webservice和RESTful Webservice
- CSS概念,引入,选择器
- react-devtool 消息处理渲染 源码理解
- Devexpress的DateEdit控件中DateTime与EditValue异同
- python—列表生成式
- update_or_create()
- jQuery 练习:取出数组字典的值, 静态对话框, clone方法应用
- 跟厂长学PHP7内核(五):系统分析生命周期
- c++ o2 优化
- hash(2018年CSUST省赛选拔赛第一场B题+hash+字典树)
- python ORM - sqlalchemy 操作使用
- Using Request Headers for Metadata Address
热门文章
- 什么是Java Marker Interface(标记接口)
- JAVA 数据库编程中的性能优化
- iOS 随机数(Fixed)
- pathForResource获取资源为nil的原因
- 机器学习(1)- 概述&;线性回归&;逻辑回归&;正则化
- Asp.Net Core 进阶(三)—— IServiceCollection依赖注入容器和使用Autofac替换它
- Caused by: java.lang.IllegalStateException: Ambiguous mapping. Cannot map &#39;userController&#39; method
- ant design table td 文字显示过长添加省略号、ant 文字过长时添加tootip提示
- 变色龙启动MAC时,错误信息“ntfs_fixup: magic doesn&#39;t match:”的解决办法
- bzoj5183 [Baltic2016]Park