[bzoj2006][NOI2010]超级钢琴

http://www.lydsy.com/JudgeOnline/problem.php?id=2006
题目大意
选择k个不同的区间使它们的和最大

首先,
我们可以把所有区间都丢到堆里,每次取出最大值
但这显然是要爆炸的。。。
然后,
可以轻易发现一个明显的性质…(很明显吗…)(Orz w_yqts)
有若干三元组(x,l,r)
l-x+1<=L,r-x+1<=r
t(x,l,r)表示左端点为x,右端点在l~r的区间中最大区间的右端点
于是我们处理完(x,l,r)之后可以继续做(x,l,t-1)和(x,t+1,r)
这样子处理显然是比较优的…
所以一开始将所有(x,x+l-1,x+r-1)丢到堆里
每次取出堆顶,然后再分裂丢回去就可以了
st表比较优秀…
代码如下…

#include <bits/stdc++.h>
using namespace std;
#define N 500005
struct node
{
    int x,l,r,t;
};
priority_queue <node> heap;
int a[N],st[N][19];
int n,K,L,R;
bool operator > (node x,node y)
{
    return a[x.t]-a[x.x-1]>a[y.t]-a[y.x-1];
}
bool operator < (node x,node y)
{
    return a[x.t]-a[x.x-1]<a[y.t]-a[y.x-1];
}
int calc(int l,int r)
{
    int len=log(1.0+r-l)/log(2.0);
    return a[st[l][len]]>a[st[r-(1<<len)+1][len]]?st[l][len]:st[r-(1<<len)+1][len];
}
int main()
{
    cin>>n>>K>>L>>R;
    for (int i=1;i<=n;++i) scanf("%d",&a[i]),a[i]+=a[i-1],st[i][0]=i;
    for (int i=1;i<=18;++i)
    for (int j=1;j+(1<<i)-1<=n;++j)
    {
    st[j][i]=a[st[j][i-1]]>a[st[j+(1<<(i-1))][i-1]]?st[j][i-1]:st[j+(1<<(i-1))][i-1];
    }
    for (int i=1;i+L-1<=n;++i)
    heap.push({i,i+L-1,min(n,i+R-1),calc(i+L-1,min(n,i+R-1))});
    long long ans=0LL;
    while (K--)
    {
        node t=heap.top();heap.pop();
        ans+=a[t.t]-a[t.x-1];
        if (t.t>t.l) heap.push({t.x,t.l,t.t-1,calc(t.l,t.t-1)});
        if (t.t<t.r) heap.push({t.x,t.t+1,t.r,calc(t.t+1,t.r)});
    }
    cout<<ans<<endl;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值