当前位置:网站首页>洛谷P2048 [NOI2010] 超级钢琴(RMQ+优先队列)
洛谷P2048 [NOI2010] 超级钢琴(RMQ+优先队列)
2022-06-25 06:43:00 【mfy的1号小迷弟】
洛谷P2048 [NOI2010] 超级钢琴(RMQ+优先队列)
题意:
有 n 个音符,编号为 1 至 n 。第 i 个音符的美妙度为 A i A_i Ai
我们要找到 k 段超级和弦组成的乐曲,每段连续的音符的个数 x 满足 L ≤ x ≤ R L\leq x\leq R L≤x≤R ,求乐曲美妙度的最大值。
思路:
三元组(x,l,r)表示左端点为x,右端点(l,r)。在设最大值的下标为t,则问题转化成求 s u m [ t ] − s u m [ i − 1 ] sum[t]-sum[i-1] sum[t]−sum[i−1],因为sum[i-1]是定值,所以求sum在(l,r)区间内的最大值
还要注意,把最大的(x,l,r)用掉后,要把(x,l,t-1)和(x,t+1,r)放进堆,因为次大值可能存在其中
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=5e5+5;
ll sum[maxn],a[maxn],ans;
int n,L,R,k,table[maxn][30];
struct node{
int s,l,r,t;
ll val;
bool operator < (const node &x)const {
return val<x.val;
}
};
priority_queue<node>p;
void init() {
for (int i = 1; i <= n; i++) table[i][0] = i;
for (int j = 1; (1 << j) <= n; j++)
for (int i = 1; i + (1 << j) - 1 <= n; i++) {
int x = table[i][j - 1], y = table[i + (1 << (j - 1))][j - 1];
table[i][j] = sum[x] > sum[y] ? x : y;
}
}
int get(int l, int r) {
int d = log2(r - l + 1);
int x = table[l][d], y = table[r - (1 << d) + 1][d];
return sum[x] > sum[y] ? x : y;
}
int main(){
scanf("%d%d%d%d",&n,&k,&L,&R);
for(int i=1;i<=n;i++){
scanf("%lld",&a[i]);
sum[i]=sum[i-1]+a[i];
}
init();
for(int i=1;i+L-1<=n;i++){
int y=min(i+R-1,n);
int d=get(i+L-1,y);
p.push({
i,i+L-1,y,d,sum[d]-sum[i-1]});
}
while(k--){
node x=p.top();
p.pop();
ans+=x.val;
if(x.t-1>=x.l){
int d1=get(x.l,x.t-1);
p.push({
x.s,x.l,x.t-1,d1,sum[d1]-sum[x.s-1]});
}
if(x.t+1<=x.r){
int d2=get(x.t+1,x.r);
p.push({
x.s,x.t+1,x.r,d2,sum[d2]-sum[x.s-1]});
}
}
printf("%lld",ans);
}
边栏推荐
- Requirements for Power PCB circuit board design 2021-11-09
- LeetCode_哈希表_中等_454.四数相加 II
- Take you through the normalization flow of GaN
- 【深度学习 轻量型backbone】2022 EdgeViTs CVPR
- Hisilicon 3559 sample parsing: Vio
- C#控件刷新
- Importer des données dans MATLAB
- 基于Anaconda的模块安装与注意事项
- 【视频】ffplay 使用mjpeg格式播放usb摄像头
- [Video] ffplay uses MJPEG format to play USB camera
猜你喜欢
STL tutorial 4- input / output stream and object serialization
Insert and sort the linked list [dummy unified operation + broken chain core - passive node]
50 pieces of professional knowledge of Product Manager (IV) - from problem to ability improvement: amdgf model tool
一次弄清楚 Handler 可能导致的内存泄漏和解决办法
C#中如何调整图像大小
挖掘微生物暗物质——新思路
【深度学习 轻量型backbone】2022 EdgeViTs CVPR
Share the process requirements for single-layer flexible circuit board
Use the frame statistics function of the message and waveform recording analyzer royalscope to troubleshoot the accidental faults of the CAN bus
Sword finger offer II 027 Palindrome linked list
随机推荐
AttributeError: ‘Upsample‘ object has no attribute ‘recompute_ scale_ factor‘
Insert and sort the linked list [dummy unified operation + broken chain core - passive node]
Machine learning notes linear regression of time series
DNS协议及其DNS完整的查询过程
MySQL simple permission management
The method of judging whether triode can amplify AC signal
Buckle 78: subset
El input to add words to the tail
How to resize an image in C #
Find out what informatization is, and let enterprises embark on the right path of transformation and upgrading
ffmpeg+SDL2实现音频播放
Advantages and differences of three kinds of vias in PCB 2021-10-27
单位转换-毫米转像素-像素转毫米
【深度学习 轻量型backbone】2022 EdgeViTs CVPR
JDBC-DAO层实现
用函数的递归来解决几道有趣的题
Ubuntu18下登录mysql 5.7设置root密码
MySQL interview - the response of executing SQL is relatively slow, and the troubleshooting ideas.
50. pow (x, n) - fast power
Understand the reasons for impedance matching of PCB circuit board 2021-10-07