洛谷 2048 BZOJ 2006 [NOI2010]超级钢琴

【题解】
贪心题。设五元组(mx,pos,l,r1,r2)表示最大值为mx,取得最大值的区间右端点为pos,区间左端点为l,区间右端点的可选区间为[r1,r2]. 每次从堆里拎出最大值,然后把这个区间拆了,因为选了[l,pos]这个区间之后就不能再选它了。我们得往堆了丢俩新的五元组(mx',pos',l,r1,pos-1)以及(mx'',pos'',l,pos+1,r2),至于mx',mx'',pos',pos''的获得,用ST表即可。
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#define N 1000010
#define rg register
#define LL long long
using namespace std;
int n,k,L,R,tot,pos[][N],f[][N];
LL ans;
struct heap{
LL d,p,l,r1,r2;
}h[N];
struct rec{
LL d,p;
};
inline int read(){
int k=,f=; char c=getchar();
while(c<''||c>'')c=='-'&&(f=-),c=getchar();
while(''<=c&&c<='')k=k*+c-'',c=getchar();
return k*f;
} inline void up(int x){
int fa;
while((fa=x>>)&&h[fa].d<h[x].d) swap(h[fa],h[x]),x=fa;
}
inline void down(int x){
int son;
while((son=x<<)<=tot){
if(son<tot&&h[son+].d>h[son].d) son++;
if(h[son].d>h[x].d) swap(h[son],h[x]),x=son;
else return;
}
}
inline rec query(int l,int r){
r=min(r,n); l=max(,l);
int k=log2(r-l+); rec tmp; tmp.d=tmp.p=;
if(l>r) return tmp;
if(f[k][l]>f[k][r-(<<k)+]){
tmp.d=f[k][l]; tmp.p=pos[k][l];
return tmp;
}
else{
tmp.d=f[k][r-(<<k)+]; tmp.p=pos[k][r-(<<k)+];
return tmp;
}
}
int main(){
memset(f,,sizeof(f)); f[][]=;
n=read(); k=read(); L=read()-; R=read()-;
for(rg int i=;i<=n;i++) f[][i]=read()+f[][i-],pos[][i]=i;
for(rg int i=;i<=log2(n);i++)
for(rg int j=;j<=n-(<<i)+;j++)
if(f[i-][j]>f[i-][j+(<<(i-))])
f[i][j]=f[i-][j],pos[i][j]=pos[i-][j];
else f[i][j]=f[i-][j+(<<(i-))],pos[i][j]=pos[i-][j+(<<(i-))];
for(rg int i=;i<=n-L;i++){
rec tmp=query(i+L,i+R);
h[++tot]=(heap){tmp.d-f[][i-],tmp.p,i,i+L,min(n,i+R)};
up(tot);
}
for(rg int i=;i<=k;i++){
heap now=h[];
ans+=now.d;
h[]=h[tot--]; if(tot) down();
if(now.p->=now.r1){
rec tmp=query(now.r1,now.p-);
h[++tot]=(heap){tmp.d-f[][now.l-],tmp.p,now.l,now.r1,now.p-};
up(tot);
}
if(now.r2>=now.p+){
rec tmp=query(now.p+,now.r2);
h[++tot]=(heap){tmp.d-f[][now.l-],tmp.p,now.l,now.p+,now.r2};
up(tot);
}
}
printf("%lld\n",ans);
return ;
}
洛谷 2048 BZOJ 2006 [NOI2010]超级钢琴的更多相关文章
- 洛谷 P2048 BZOJ 2006 [NOI2010]超级钢琴
题目描述 小Z是一个小有名气的钢琴家,最近C博士送给了小Z一架超级钢琴,小Z希望能够用这架钢琴创作出世界上最美妙的音乐. 这架超级钢琴可以弹奏出n个音符,编号为1至n.第i个音符的美妙度为Ai,其中A ...
- BZOJ 2006: [NOI2010]超级钢琴
2006: [NOI2010]超级钢琴 Time Limit: 20 Sec Memory Limit: 552 MBSubmit: 2613 Solved: 1297[Submit][Statu ...
- Bzoj 2006: [NOI2010]超级钢琴 堆,ST表
2006: [NOI2010]超级钢琴 Time Limit: 20 Sec Memory Limit: 552 MBSubmit: 2222 Solved: 1082[Submit][Statu ...
- BZOJ 2006: [NOI2010]超级钢琴( RMQ + 堆 )
取最大的K个, 用堆和RMQ来加速... ----------------------------------------------------------------- #include<c ...
- BZOJ.2006.[NOI2010]超级钢琴(贪心 堆)
BZOJ 洛谷 思路和BZOJ3784一样,用前缀和+堆维护.做那题吧,不赘述啦. (没错我就是水一个AC) //54620kb 1060ms #include <queue> #incl ...
- BZOJ 2006 [NOI2010]超级钢琴 (堆+主席树)
题面:BZOJ传送门 洛谷传送门 让你求前$K$大的子序列和,$n\leq 5*10^{5}$ 只想到了个$nlog^{2}n$的做法,似乎要被卡常就看题解了.. 好神奇的操作啊,我傻了 我们把序列和 ...
- bzoj 2006 [NOI2010]超级钢琴——ST表+堆
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2006 每个右端点的左端点在一个区间内:用堆记录端点位置.可选区间,按价值排序:拿出一个后也许 ...
- BZOJ 2006 NOI2010 超级钢琴 划分树+堆
题目大意:给定一个序列.找到k个长度在[l,r]之间的序列.使得和最大 暴力O(n^2logn),肯定过不去 看到这题的第一眼我OTZ了一下午... 后来研究了非常久别人的题解才弄明确怎么回事...蒟 ...
- BZOJ 2006: [NOI2010]超级钢琴 [ST表+堆 | 主席树]
题意: 一个序列,求k个不相同的长度属于\([L,R]\)的区间使得和最大 前缀和,对于每个r找最小的a[l] 然后我yy了一个可持久化线段树做法...也许会T 实际上主席树就可以了,区间k小值 然后 ...
随机推荐
- LOJ 6089 小Y的背包计数问题 —— 前缀和优化DP
题目:https://loj.ac/problem/6089 对于 i <= √n ,设 f[i][j] 表示前 i 种,体积为 j 的方案数,那么 f[i][j] = ∑(1 <= k ...
- RDA Kconfig介绍 ***
安装显示库: sudo apt-get install libncurses5-dev libncursesw5-dev 执行相关文件: 1.radisson.Kconfig 2.gen_radiss ...
- 基于Spark和Tensorflow构建DCN模型进行CTR预测
实验介绍 数据采用Criteo Display Ads.这个数据一共11G,有13个integer features,26个categorical features. Spark 由于数据比较大,且只 ...
- [Swift通天遁地]一、超级工具-(19)制作六种别具风格的动作表单
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- c++病毒函数
FreeConsole(); 屏蔽输出. BlockInput(); 阻止键盘和鼠标的工作. 所需头文件: #include <windows.h> #include <Winabl ...
- Net 发布网站中遇到的几点问题
1.windows 身份验证设置 打开IIS==>=>找到网站==> 身份验证==>打开功能==>启用windows身份验证 网站设置: 博客参考: http://blo ...
- D. Vasya And The Matrix(Educational Codeforces Round 48)
D. Vasya And The Matrix time limit per test2 seconds memory limit per test256 megabytes inputstandar ...
- 有关lower_bound()函数的使用
lower_bound()函数需要加载头文件#include<algorithm>,其基本用途是查找有序区间中第一个大于或等于某给定值的元素的位置,其中排序规则可以通过二元关系来表示. 函 ...
- mysqlbinlog(日志管理工具)
mysqlbinlog用于处理二进制的日志文件,如果想要查看这些日志文件的文本内容,就需要使用mysqlbinlog工具. 1.mysqlbinlog命令的语法 shell > mysqlbin ...
- WinForm ListBox 控件用法
下面演示如何利用列表控件 ListBox 实现多选与移动选项: using IMS.WinFormClient.UserControls; using System; using System.Col ...