【贪心 计数】bzoj2006: [NOI2010]超级钢琴
这么经典的贪心我怎么现在才做啊……
Description
Input
Output
只有一个整数,表示乐曲美妙度的最大值。
Sample Input
3
2
-6
8
Sample Output
题目分析
定位:是一道堆例题
首先注意到k的规模不大,那么可以枚举k次。
考虑固定右端点,那么可以预处理出它合法的左端点以及前缀和最小的位置。

因为不能重复,所以用过的$l$对$r$不能再用。那么如果$r$再一次被当做右端点,$l'$要么在$l$左边要么在$l$右边。因此再把剩下的两个状态处理出来就可以了。
状态用一个五元组保存就行了;查询静态最优值则可以用st表预处理。
#include<bits/stdc++.h>
typedef long long ll;
const int maxn = ;
const int maxLog = ; struct node
{
int L,R,i,v,p;
node(int a=, int b=, int c=, int d=, int e=):L(a),R(b),i(c),v(d),p(e) {}
bool operator < (node a) const
{
return v < a.v;
}
};
int n,k,L,R,s[maxn],lgs[maxn],f[maxn][maxLog];
std::priority_queue<node> q;
ll ans; int read()
{
char ch = getchar();
int num = ;
bool fl = ;
for (; !isdigit(ch); ch=getchar())
if (ch=='-') fl = ;
for (; isdigit(ch); ch=getchar())
num = (num<<)+(num<<)+ch-;
if (fl) num = -num;
return num;
}
void STinit()
{
for (int i=; i<=n; i++) f[i][] = i; //这里要处理i=0,因为查询时候的[l',r']是[l,r]的前缀位置,即[l-1,r-1]
for (int j=; (<<j)<=n; j++)
for (int i=; i<=n; i++)
if (i+(<<j) < n){
int l = f[i][j-], r = f[i+(<<(j-))][j-];
if (s[l] > s[r]) f[i][j] = r;
else f[i][j] = l;
}
}
int STquery(int l, int r)
{
l--, r--;
int c = lgs[r-l+], x = f[l][c], y = f[r-(<<c)+][c];
return s[x] > s[y]?y:x;
}
int main()
{
n = read(), k = read(), L = read(), R = read(), lgs[] = -;
for (int i=; i<=n; i++) s[i] = s[i-]+read();
for (int i=; i<=n; i++) lgs[i] = lgs[i>>]+;
STinit();
for (int i=; i<=n; i++)
{
int lbd = std::max(i-R+, ), rbd = std::max(i-L+, );
if (i-rbd+ < L) continue;
int pos = STquery(lbd, rbd);
q.push(node(lbd, rbd, i, s[i]-s[pos], pos+));
}
while (k--)
{
node tt = q.top();
q.pop(), ans += tt.v;
int lbd = tt.L, rbd = tt.R, i = tt.i, p = tt.p, pos;
if (lbd < p){
pos = STquery(lbd, p-);
q.push(node(lbd, p-, i, s[i]-s[pos], pos+));
}
if (rbd > p){
pos = STquery(p+, rbd);
q.push(node(p+, rbd, i, s[i]-s[pos], pos+));
}
}
printf("%lld\n",ans);
return ;
}
END
【贪心 计数】bzoj2006: [NOI2010]超级钢琴的更多相关文章
- bzoj2006 [NOI2010]超级钢琴 (及其拓展)
bzoj2006 [NOI2010]超级钢琴 给定一个序列,求长度在 \([L,\ R]\) 之间的区间和的前 \(k\) 大之和 \(n\leq5\times10^5,\ k\leq2\times1 ...
- [BZOJ2006] [NOI2010]超级钢琴 主席树+贪心+优先队列
2006: [NOI2010]超级钢琴 Time Limit: 20 Sec Memory Limit: 552 MBSubmit: 3591 Solved: 1780[Submit][Statu ...
- [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]超级钢琴 【堆 + RMQ】
2006: [NOI2010]超级钢琴 Time Limit: 20 Sec Memory Limit: 552 MB Submit: 3446 Solved: 1692 [Submit][Sta ...
- bzoj2006 noi2010 超级钢琴 主席树 + 优先队列
Time Limit: 20 Sec Memory Limit: 552 MBSubmit: 2435 Solved: 1195 Description 小 Z是一个小有名气的钢琴家,最近C博士送 ...
- BZOJ2006[NOI2010]超级钢琴——堆+主席树
题目描述 小Z是一个小有名气的钢琴家,最近C博士送给了小Z一架超级钢琴,小Z希望能够用这架钢琴创作出世界上最美妙的 音乐. 这架超级钢琴可以弹奏出n个音符,编号为1至n.第i个音符的美妙度为Ai,其中 ...
- 【题解】 bzoj2006: [NOI2010]超级钢琴 (ST表+贪心)
题面戳我 Solution 不会,看的题解 Attention 哇痛苦,一直不会打\(ST\)表,我是真的菜啊qwq 预处理 Log[1]=0;two[0]=1; for(int i=2;i<= ...
- BZOJ2006——[NOI2010]超级钢琴
1.题意:给一个序列,让你取出k个不同的区间,要求长度在之间,问所有区间和的最大值 2.分析:这道题拿过来就能知道是要拿出前k个最大的区间,我们思考最暴力的做法,就是把这个所有的区间枚举出来算,取出前 ...
随机推荐
- Centos7.x 安装 pptp
VPN 1:检查是否支持PPTP #返回OK && echo ok ok 2:安装ppp yum install -y ppp 3:导入EPEL源 rpm -ivh http://dl ...
- Python-9-赋值进阶
1.序列解包 同时给多个变量赋值 >>> x, y, z = 1, 2, 3 >>> print(x, y, z) 1 2 3 用这种方式还可以交换两个变量的值 ...
- js 获取两个日期相差的天数--自定义方法
//获取两个日期的相差天数 datedifference=function(sDate1, sDate2) { var dateSpan, tempDate, iDays; sDate1 = Date ...
- Codeforces Round #565 (Div. 3) C. Lose it!
链接: https://codeforces.com/contest/1176/problem/C 题意: You are given an array a consisting of n integ ...
- CoreRT
使用CoreRT将.NET Core发布为Native应用程序 在上一篇文章<使用.NET Core快速开发一个较正规的命令行应用程序>中我们看到了使用自包含方式发布的.NET Core应 ...
- 安装Jaspersoft Studio
下载位置:http://community.jaspersoft.com/project/jaspersoft-studio/releases.
- java lombok包在maven已经配置,但是注解没用
如果你是用eclipse作为开发环境,配置了maven依赖以后,还需要在eclipse/myeclipse中手动安装lombok. 其实就是加一个jar包,添加2行代码 1. 将 lombok.jar ...
- React Router 4.0中文快速入门
import React from 'react' import { BrowserRouter as Router, Route, Link } from 'react-router-dom' co ...
- c#基础 base和this的区别,在继承上面
base public Person(string name, int age, char gender) { this.Name = name; this.Age = age; this.Gende ...
- String类、StringBuilder类、StringBuffer类
String类是不可变类,创建了一个String对象后,该String对象中的字符序列不能被改变. StringBuilder是字符序列可变的字符串类,提供了一系列修改字符串的方法. StringBu ...