[BZOJ2006] [NOI2010]超级钢琴 主席树+贪心+优先队列
2006: [NOI2010]超级钢琴
Time Limit: 20 Sec Memory Limit: 552 MB
Submit: 3591 Solved: 1780
[Submit][Status][Discuss]
Description
Input
Output
只有一个整数,表示乐曲美妙度的最大值。
Sample Input
3
2
-6
8
Sample Output
【样例说明】
共有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。
HINT
Source
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<cstdio>
#include<queue>
#define maxn 500055
#define ll long long
using namespace std;
int sum[maxn*],ls[maxn*],rs[maxn*],rt[maxn],cnt;
int n,K,L,R;
int pre[maxn],h[maxn],num[maxn],size[maxn];
struct data {
int w,pos;
bool operator <(const data tmp) const{return w<tmp.w;}
};
priority_queue<data> q;
void insert(int &x,int p,int l,int r,int k) {
x=++cnt;
sum[x]=sum[p]+,ls[x]=ls[p],rs[x]=rs[p];
if(l==r) {return;}
int mid=l+r>>;
if(k<=mid) insert(ls[x],ls[p],l,mid,k);
else insert(rs[x],rs[p],mid+,r,k);
}
int find(int x,int p,int l,int r,int k) {
// cout<<l<<' '<<r<<' '<<sum[ls[p]]<<' '<<sum[ls[x]]<<endl;
if(l==r) return l;
int mid=l+r>>;
if(sum[ls[p]]-sum[ls[x]]>=k) return find(ls[x],ls[p],l,mid,k);
else return find(rs[x],rs[p],mid+,r,k-(sum[ls[p]]-sum[ls[x]]));
}
int main() {
scanf("%d%d%d%d",&n,&K,&L,&R);
h[n+]=;
for(int i=;i<=n;i++) {
int tmp;scanf("%d",&tmp);
pre[i]=pre[i-]+tmp;
h[i]=pre[i];
}
sort(h+,h+n+);
int nn=;
for(int i=;i<=n+;i++) if(h[i]!=h[nn]) h[++nn]=h[i];
int hh=lower_bound(h+,h+nn+,)-h;
insert(rt[],rt[],,n,hh);
for(int i=;i<=n;i++) {
num[i]=lower_bound(h+,h+nn+,pre[i])-h;
insert(rt[i+],rt[i],,n,num[i]);
}
for(int i=L;i<=n;i++) {
if(i-L<) continue;
int w=find(rt[max(i-R,)],rt[max(i-L+,)],,n,size[i]+);
q.push((data){pre[i]-h[w],i});
}
ll ans=;
for(int i=;i<=K;i++) {
int w=q.top().w,pos=q.top().pos;
ans+=(ll)w;
size[pos]++;
q.pop();
int l=max(pos-R+,),r=pos-L+;
if(size[pos]>=r-l+) continue;
w=find(rt[l-],rt[r],,n,size[pos]+);
w=pre[pos]-h[w];
q.push((data){w,pos});
}
printf("%lld\n",ans);
}
[BZOJ2006] [NOI2010]超级钢琴 主席树+贪心+优先队列的更多相关文章
- bzoj2006 noi2010 超级钢琴 主席树 + 优先队列
Time Limit: 20 Sec Memory Limit: 552 MBSubmit: 2435 Solved: 1195 Description 小 Z是一个小有名气的钢琴家,最近C博士送 ...
- [NOI2010]超级钢琴 主席树
[NOI2010]超级钢琴 链接 luogu 思路 和12省联考的异或粽子一样. 堆维护n个左端点,每次取出来再放回去次 代码 #include <bits/stdc++.h> #defi ...
- bzoj2006 [NOI2010]超级钢琴 (及其拓展)
bzoj2006 [NOI2010]超级钢琴 给定一个序列,求长度在 \([L,\ R]\) 之间的区间和的前 \(k\) 大之和 \(n\leq5\times10^5,\ k\leq2\times1 ...
- BZOJ2006 [NOI2010]超级钢琴 【堆 + RMQ】
2006: [NOI2010]超级钢琴 Time Limit: 20 Sec Memory Limit: 552 MB Submit: 3446 Solved: 1692 [Submit][Sta ...
- [BZOJ2006][NOI2010]超级钢琴(ST表+堆)
2006: [NOI2010]超级钢琴 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 3679 Solved: 1828[Submit][Statu ...
- bzoj千题计划162:bzoj2006: [NOI2010]超级钢琴
http://www.lydsy.com/JudgeOnline/problem.php?id=2006 输出最大的k个 sum[r]-sum[l-1] (L<=r-l+1<=R) 之和 ...
- BZOJ2006[NOI2010]超级钢琴——堆+主席树
题目描述 小Z是一个小有名气的钢琴家,最近C博士送给了小Z一架超级钢琴,小Z希望能够用这架钢琴创作出世界上最美妙的 音乐. 这架超级钢琴可以弹奏出n个音符,编号为1至n.第i个音符的美妙度为Ai,其中 ...
- [NOI2010][bzoj2006] 超级钢琴 [主席树/ST表+堆]
题面: 传送门 思路: 首先容易想到用堆维护的O(n2logn)暴力 那么肯定就是在这个基础上套数据结构了[愉快] 然而我因为过于蒟蒻......只想得到主席树暴力***过去的方法 大概就是把前缀和算 ...
- 【贪心 计数】bzoj2006: [NOI2010]超级钢琴
这么经典的贪心我怎么现在才做啊…… Description 小Z是一个小有名气的钢琴家,最近C博士送给了小Z一架超级钢琴,小Z希望能够用这架钢琴创作出世界上最美妙的 音乐. 这架超级钢琴可以弹奏出n个 ...
随机推荐
- hdu 1285 确定比赛名次 (拓扑)
确定比赛名次 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Subm ...
- LeetCode -- 3SumCloset
Question: Given an array S of n integers, find three integers in S such that the sum is closest to a ...
- Event loop的macro task和micro task
macrotask在一些文章中也被直接称为task. 一个宿主环境只有一个事件循环,但可以有多个任务队列.宏任务队列(macro task)与微任务队列(micro task)就是其中之二. 每次事件 ...
- HDU - 1880 魔咒词典~哈希入门
哈利波特在魔法学校的必修课之一就是学习魔咒.据说魔法世界有100000种不同的魔咒,哈利很难全部记住,但是为了对抗强敌,他必须在危急时刻能够调用任何一个需要的魔咒,所以他需要你的帮助. 给你一部魔咒词 ...
- BAT定期删除N天前的文件
1.直接看脚本在win2008测试可用 ::clean logs @echo off title clean up logs ::delete logs FORFILES /P /C "cm ...
- hdu 6223 Infinite Fraction Path
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=6223 题意:给定长度为n的一串数字S,现在要按照一种规则寻找长度为n的数字串,使得该数字串的字典序最大 ...
- 浏览器web端详解
一.背景介绍浏览器是前端工程师或页面重构师工作中必不可少的,WEB页面运行在各种各样的浏览器当中,浏览器载入.渲染页面的速度直接影响着用户体验,特别是浏览器渲染页面的原理,页面渲染就是浏览器将HTML ...
- BZOJ 2063: 我爸是李刚
2063: 我爸是李刚 Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 155 Solved: 82[Submit][Status][Discuss] ...
- I wrote a JSONHelper extension
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Newtonso ...
- [bzoj2124]等差子序列——线段树+字符串哈希
题目大意 给一个1到N的排列\(A_i\),询问是否存在\(p_i\),\(i>=3\),使得\(A_{p_1}, A_{p_2}, ... ,A_{p_len}\)是一个等差序列. 题解 显然 ...