【NOI2010】【BZOJ2006】超級鋼琴
來源:程序員人生 發布時間:2016-04-13 08:11:46 閱讀次數:2435次
Description
小Z是1個小著名氣的鋼琴家,最近C博士送給了小Z1架超級鋼琴,小Z希望能夠用這架鋼琴創作出世界上最美好的音樂。 這架超級鋼琴可以彈奏出n個音符,編號為1至n。第i個音符的美好度為Ai,其中Ai可正可負。 1個“超級和弦”由若干個編號連續的音符組成,包括的音符個數很多于L且不多于R。我們定義超級和弦的美好度為其包括的所有音符的美好度之和。兩個超級和弦被認為是相同的,當且僅當這兩個超級和弦所包括的音符集合是相同的。 小Z決定創作1首由k個超級和弦組成的樂曲,為了使得樂曲更加動聽,小Z要求該樂曲由k個不同的超級和弦組成。我們定義1首樂曲的美好度為其所包括的所有超級和弦的美好度之和。小Z想知道他能夠創作出來的樂曲美好度最大值是多少。
Input
第1行包括4個正整數n, k, L, R。其中n為音符的個數,k為樂曲所包括的超級和弦個數,L和R分別是超級和弦所包括音符個數的下限和上限。 接下來n行,每行包括1個整數Ai,表示按編號從小到大每一個音符的美好度。
Output
只有1個整數,表示樂曲美好度的最大值。
Sample Input
4 3 2 3
3
2
⑹
8
Sample Output
11
【樣例說明】
共有5種不同的超級和弦:
音符1 ~ 2,美好度為3 + 2 = 5
音符2 ~ 3,美好度為2 + (⑹) = ⑷
音符3 ~ 4,美好度為(⑹) + 8 = 2
音符1 ~ 3,美好度為3 + 2 + (⑹) = ⑴
音符2 ~ 4,美好度為2 + (⑹) + 8 = 4
最優方案為:樂曲由和弦1,和弦3,和弦5組成,美好度為5 + 2 + 4 = 11。
HINT數據范圍及提示 Data Size & Hint
0< N<=500000,0< k<=50000
所有數據滿足:⑴000 ≤ Ai ≤ 1000,1 ≤ L ≤ R ≤ n且保證1定存在滿足要求的樂曲。
Source
Day1
斟酌用3元組[l,r1,r2]表示在所有區間[l,r1],[l,r1+1],…,[l,r2]這些區間里區間和最大值的最大值,并記錄取到最大值的位置.線段樹保護1個前綴和加上那個位置就行.
然后將每一個[i,i+l⑴,i+r⑴]加入優先隊列,k次取優先隊列頂元素積累答案,如果取到最大值的位置pos,再將[i, pos+1, i+r⑴]和[i,i+l⑴, pos⑴]重新加入堆.(他們也有可能成為新的最大元素).
#include #include #include #include #include #include #define MAXN 500100 #define GET (ch>=0&&ch<=9) #define lchild rt<<1,l,mid #define rchild rt<<1|1,mid+1,r #define ln rt<<1 #define rn rt<<1|1 #define LL long long using namespace std;
LL n,k,_l,_r;
LL ans,a[MAXN];
struct seg
{
int l,r;
LL maxn,pos;
}tree[MAXN<<
2];
struct node
{
int i,l,r,maxn,pos;
inline
bool operator <(
const node& a)
const {
return maxn
void push_up(int rt)
{ if (tree[ln].maxn<=tree[rn].maxn) tree[rt].maxn=tree[rn].maxn,tree[rt].pos=tree[rn].pos; else tree[rt].maxn=tree[ln].maxn,tree[rt].pos=tree[ln].pos;
} void build(int rt=1,int l=1,int r=n)
{
tree[rt].l=l;tree[rt].r=r;int mid=l+r>>1; if (l==r) {tree[rt].maxn=a[l];tree[rt].pos=l;return;}
build(lchild);build(rchild);push_up(rt);
}
seg query(int rt,int l,int r)
{ int L=tree[rt].l,R=tree[rt].r,mid=L+R>>1;seg ret;ret.maxn=ret.pos=0; if (l<=L&&R<=r) return tree[rt]; if (r<=mid) return query(ln,l,r); if (l>mid) return query(rn,l,r);
seg a=query(ln,l,mid),b=query(rn,mid+1,r);ret=a.maxn>b.maxn?a:b; return ret;
} void in(long long &x)
{
x=0;char ch=getchar();int flag=1; while (!GET) flag=ch==-?-1:1,ch=getchar(); while (GET) x=x*10+ch-0,ch=getchar();x*=flag;
} int main()
{ in(n);in(k);in(_l);in(_r);node tmp,temp;seg t; for (int i=1;i<=n;i++) in(a[i]),a[i]=a[i-1]+a[i];build(); for (int i=1;i<=n;i++) {
temp.i=i;temp.l=i+_l-1;temp.r=min(i+_r-1,n);if (temp.l>n) continue;
t=query(1,temp.l,temp.r);temp.maxn=t.maxn-a[i-1];temp.pos=t.pos;heap.push(temp);
} for (int i=1;i<=k;i++) {
temp=heap.top();heap.pop();ans+=temp.maxn; if (temp.l1;
t=query(1,tmp.l,tmp.r);tmp.maxn=t.maxn-a[tmp.i-1];tmp.pos=t.pos;heap.push(tmp);
} if (temp.r>temp.pos)
{
tmp.i=temp.i;tmp.l=temp.pos+1;tmp.r=temp.r;
t=query(1,tmp.l,tmp.r);tmp.maxn=t.maxn-a[tmp.i-1];tmp.pos=t.pos;heap.push(tmp);
}
}
cout<
生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈