bzoj 2006: [NOI2010]超级钢琴

Description

小Z是一个小有名气的钢琴家,最近C博士送给了小Z一架超级钢琴,小Z希望能够用这架钢琴创作出世界上最美妙的音乐。 这架超级钢琴可以弹奏出n个音符,编号为1至n。第i个音符的美妙度为Ai,其中Ai可正可负。 一个“超级和弦”由若干个编号连续的音符组成,包含的音符个数不少于L且不多于R。我们定义超级和弦的美妙度为其包含的所有音符的美妙度之和。两个超级和弦被认为是相同的,当且仅当这两个超级和弦所包含的音符集合是相同的。 小Z决定创作一首由k个超级和弦组成的乐曲,为了使得乐曲更加动听,小Z要求该乐曲由k个不同的超级和弦组成。我们定义一首乐曲的美妙度为其所包含的所有超级和弦的美妙度之和。小Z想知道他能够创作出来的乐曲美妙度最大值是多少。

Input

第一行包含四个正整数n, k, L, R。其中n为音符的个数,k为乐曲所包含的超级和弦个数,L和R分别是超级和弦所包含音符个数的下限和上限。 接下来n行,每行包含一个整数Ai,表示按编号从小到大每个音符的美妙度。

Output

只有一个整数,表示乐曲美妙度的最大值。

Sample Input

4 3 2 3

3

2

-6

8

Sample Output

11

【样例说明】
共有5种不同的超级和弦:

音符1 ~ 2,美妙度为3 + 2 = 5
音符2 ~ 3,美妙度为2 + (-6) = -4
音符3 ~ 4,美妙度为(-6) + 8 = 2
音符1 ~ 3,美妙度为3 + 2 + (-6) = -1
音符2 ~ 4,美妙度为2 + (-6) + 8 = 4
最优方案为:乐曲由和弦1,和弦3,和弦5组成,美妙度为5 + 2 + 4 = 11。

这题做法还是非常神奇的
用s[]表示a[]的前缀和。那么和弦的值我们可以用s[]来表示了。
因为k的值很大。所以不可能朴素枚举
考虑哪些和弦会对ans有贡献。
我们可以用堆按照x维护一个五元组(s,l,r,d,x),表示s为起点,可以到达区间[l,r],中间最大值为x,位置为d。
每次从堆顶取出一个元素把x加入答案
然后我们把这个元素分割成(s,l,d-1,d1,x1)和(s,d+1,r,d2,x2)
最大值用线段树或者ST表维护下就可以了
#include<queue>
#include<vector>
#include<cstdio>
#include<algorithm>
using namespace std;
long long inf=(long long)100000000*(long long)100000;
struct tree
{
     int l,r;
     int loc;
     long long m;
}tr[4000001];
long long a[500001];
long long s[500001];
inline void up(int p)
{
     if(tr[p*2].m>tr[p*2+1].m)
     {
          tr[p].m=tr[p*2].m;
          tr[p].loc=tr[p*2].loc;
     }
     else
     {
          tr[p].m=tr[p*2+1].m;
          tr[p].loc=tr[p*2+1].loc;
     }
}
inline void build(int p,int l,int r)
{
     tr[p].l=l;
     tr[p].r=r;
     if(l!=r)
     {
          int mid=(l+r)/2;
          build(p*2,l,mid);
          build(p*2+1,mid+1,r);
          up(p);
     }
     else
     {
          tr[p].m=s[l];
          tr[p].loc=l;
     }
}
tree nw;
inline tree ask(int p,int l,int r)
{
     if(l<=tr[p].l&&tr[p].r<=r)
          return tr[p];
     else
     {
          int mid=(tr[p].l+tr[p].r)/2;
          tree ans1=nw,ans2=nw,as=nw;
          bool flag1=false,flag2=false;
          if(l<=mid)
          {
               flag1=true;
               ans1=ask(p*2,l,r);
          }
          if(r>mid)
          {
          	   flag2=true;
               ans2=ask(p*2+1,l,r);
          }
          if(flag1)
          {
               if(flag2)
               {
                    if(ans1.m>ans2.m)
                    {
                         as.m=ans1.m;
                         as.loc=ans1.loc;
                    }
                    else
                    {
                         as.m=ans2.m;
                         as.loc=ans2.loc;
                    }
               }
               else
                    as=ans1;
          }
          else
               as=ans2;
          return as;
     }
}
long long ans;
struct findx
{
	 int s;
	 int d;
     int l,r;
     long long x;
     bool operator <(findx y) const
     {
          return x<y.x;
     }
};
priority_queue <findx> Q;
int k;
inline void solve()
{
     int p=1;
     while(p<=k)
     {
     	  findx t=Q.top();
     	  Q.pop();
          ans+=t.x;
          findx t1,t2;
          tree x;
          if(t.d-1>=t.l)
          {
               x=ask(1,t.l,t.d-1);
               t1.s=t.s;
               t1.l=t.l;
               t1.r=t.d-1;
               t1.x=x.m-s[t.s-1];
               t1.d=x.loc;
               Q.push(t1);
          }
          if(t.d+1<=t.r)
          {
               x=ask(1,t.d+1,t.r);
               t2.s=t.s;
               t2.l=t.d+1;
               t2.r=t.r;
               t2.x=x.m-s[t.s-1];
               t2.d=x.loc;
               Q.push(t2);
          }
          p++;
     }
}
int main()
{
//	 freopen("piano.in","r",stdin);
//	 freopen("piano.out","w",stdout);
     int n,l,r;
     scanf("%d%d%d%d",&n,&k,&l,&r);
     int i;
     for(i=1;i<=n;i++)
     {
          scanf("%lld",&a[i]);
          s[i]=s[i-1]+a[i];
     }
     build(1,1,n);
     while(!Q.empty())
          Q.pop();
     for(i=1;i<=n;i++)
     {
     	  int ss=i+l-1,tt=i+r-1;
     	  if(ss>n)
     	       continue;
     	  if(tt>n)
     	       tt=n;
     	  tree x=ask(1,ss,tt);
     	  findx t;
     	  t.s=i;
     	  t.d=x.loc;
		  t.l=ss;
		  t.r=tt;
		  t.x=x.m-s[i-1];
          Q.push(t);
     }
     solve();
     printf("%lld\n",ans);
     return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值