当前位置:网站首页>洛谷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);
}
边栏推荐
- 420-二叉树的层序遍历2(429. N 叉树的层序遍历、515.在每个树行中找最大值、116.填充每个节点的下一个右侧节点指针、104.二叉树的最大深度、111.二叉树的最小深度)
- 网络模型——OSI模型与TCP/IP模型
- Anaconda based module installation and precautions
- 2265. number of nodes with statistical value equal to the average value of subtree
- 个人域名和企业域名的区别
- 417-二叉树的层序遍历1(102. 二叉树的层序遍历、107.二叉树的层次遍历 II、199.二叉树的右视图、637.二叉树的层平均值)
- The method of judging whether triode can amplify AC signal
- 饮食干预减轻癌症治疗相关症状和毒性
- C#控件刷新
- 将数据导入到MATLAB
猜你喜欢

CAN透传云网关CANIOT,CANDTU记录CAN报文远程收发CAN数据

Import data into Matlab

NPM install reports an error: gyp err! configure error

消息中间件之ActiveMQ的基本使用

【深度学习 轻量型backbone】2022 EdgeViTs CVPR

机器学习笔记 - 时间序列的线性回归

ts环境搭建

微信小程序开通客服消息功能开发

Introduction to the main functions of the can & canfd comprehensive test and analysis software lkmaster of the new usbcan card can analyzer

Three Siemens fire-fighting hosts fc18 are equipped with can optical transceiver for optical fiber redundant ring network networking test
随机推荐
挖掘微生物暗物质——新思路
Keil and Proteus joint commissioning
ts环境搭建
云计算考试版本1.0
1742. maximum number of small balls in the box
STL tutorial 4- input / output stream and object serialization
CAN总线工作状况和信号质量“体检”
Different paths ii[dynamic planning improvement for DFS]
Advantages and differences of three kinds of vias in PCB 2021-10-27
Force deduction 76 questions, minimum covering string
El input to add words to the tail
Terms and concepts related to authority and authentication system
MySQL interview - the response of executing SQL is relatively slow, and the troubleshooting ideas.
27. 移除元素
1464. maximum product of two elements in an array
Use the frame statistics function of the message and waveform recording analyzer royalscope to troubleshoot the accidental faults of the CAN bus
Manufacturing process of PCB 2021-10-11
差点被这波Handler 面试连环炮带走~
Technology blog | how to communicate using SSE
神经网络与深度学习-3- 机器学习简单示例-PyTorch