bzoj2600 [Ioi2011]ricehub 双指针
题目传送门
https://lydsy.com/JudgeOnline/problem.php?id=2600
题解
随便写一个比较简单的 two pointers 练习题。
首先答案肯定是一个原序列上的区间。然后一个区间的费用——显然选择的点应该是这个区间的中位数(初中数学)。
然后一个区间的费用肯定比其子区间费用要大。
所以可以直接 two-pointers 来做。
时间复杂度 \(O(n)\)。
#include<bits/stdc++.h>
#define fec(i, x, y) (int i = head[x], y = g[i].to; i; i = g[i].ne, y = g[i].to)
#define dbg(...) fprintf(stderr, __VA_ARGS__)
#define File(x) freopen(#x".in", "r", stdin), freopen(#x".out", "w", stdout)
#define fi first
#define se second
#define pb push_back
template<typename A, typename B> inline char smax(A &a, const B &b) {return a < b ? a = b, 1 : 0;}
template<typename A, typename B> inline char smin(A &a, const B &b) {return b < a ? a = b, 1 : 0;}
typedef long long ll; typedef unsigned long long ull; typedef std::pair<int, int> pii;
template<typename I> inline void read(I &x) {
int f = 0, c;
while (!isdigit(c = getchar())) c == '-' ? f = 1 : 0;
x = c & 15;
while (isdigit(c = getchar())) x = (x << 1) + (x << 3) + (c & 15);
f ? x = -x : 0;
}
const int N = 100000 + 7;
int n, m;
ll b;
int a[N];
ll s[N];
inline ll get(int l, int r) {
int mid = (l + r) >> 1;
return s[r] - s[mid] - (ll)(r - mid) * a[mid] + (ll)(mid - l) * a[mid] - (s[mid - 1] - s[l - 1]);
}
inline void work() {
int p = 1, ans = 0;
for (int i = 1; i <= n; ++i) {
smax(p, i);
while (p < n && get(i, p + 1) <= b) ++p;
smax(ans, p - i + 1);
}
printf("%d\n", ans);
}
inline void init() {
read(n), read(m), read(b);
for (int i = 1; i <= n; ++i) read(a[i]), s[i] = s[i - 1] + a[i];
}
int main() {
#ifdef hzhkk
freopen("hkk.in", "r", stdin);
#endif
init();
work();
fclose(stdin), fclose(stdout);
return 0;
}
bzoj2600 [Ioi2011]ricehub 双指针的更多相关文章
- bzoj2600 [Ioi2011]ricehub
Description 乡间有一条笔直而长的路称为“米道”.沿着这条米道上 R 块稻田,每块稻田的坐标均为一个 1 到 L 之间(含 1 和 L)的整数.这些稻田按照坐标以不减的顺序给出,即对于 0 ...
- BZOJ 2600: [Ioi2011]ricehub 双指针+贪心
不难发现,当我们要选的区间确定后,一定会把仓库安排到中间的稻草上(如果是偶数个的话中间两个都行). 然后按照坐标从小到大枚举右指针,左指针一定不递减,双指针扫一下就行了. code: #include ...
- [bzoj2600][Ioi2011]ricehub_二分
ricehub bzoj-2600 Ioi-2011 题目大意:在数轴上有r块稻田,稻田坐标为整数.计划建造一个米仓,使得它可以收取尽量多的稻米.米仓的坐标仍需为整数.每一块权值为val的稻田距离米仓 ...
- BZOJ 2600: [Ioi2011]ricehub
2600: [Ioi2011]ricehub Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 628 Solved: 325[Submit][Stat ...
- 【bzoj2600】[Ioi2011]ricehub 双指针法
题目描述 给出数轴上坐标从小到大的 $R$ 个点,坐标范围在 $1\sim L$ 之间.选出一段连续的点,满足:存在一个点,使得所有选出的点到其距离和不超过 $B$ .求最多能够选出多少点. $R\l ...
- 【bzoj2600】 [Ioi2011]ricehub
如果发现尾指针到头指针这段稻田的中位数上建一个粮仓时距离之和超过了B 就调整尾指针对距离维护一个前缀和 每次取中位数之后可以O(1)计算距离和 #include<algorithm> #i ...
- [Ioi2011]ricehub
Description 乡间有一条笔直而长的路称为“米道”.沿着这条米道上 R 块稻田,每块稻田的坐标均 为一个 1 到 L 之间(含 1 和 L)的整数.这些稻田按照坐标以不减的顺序给出,即对于 0 ...
- bzoj 2600 ricehub
2600: [Ioi2011]ricehub Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 783 Solved: 417[Submit][Stat ...
- lydsy 2600(二分+中位数前缀和)米仓
2600: [Ioi2011]ricehub Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 691 Solved: 359[Submit][Stat ...
随机推荐
- postgres 数据导入导出
# 导出表结构 pg_dump -U postgres -s helloworld > hello.sql # 导出表数据 pg_dump -U postgres helloworld & ...
- Git-Runoob:Git 创建仓库
ylbtech-Git-Runoob:Git 创建仓库 1.返回顶部 1. Git 创建仓库 本章节我们将为大家介绍如何创建一个 Git 仓库. 你可以使用一个已经存在的目录作为Git仓库. git ...
- HAProxy & Keepalived L4-L7 高可用负载均衡解决方案
目录 文章目录 目录 HAProxy 负载均衡器 应用特性 性能优势 会话保持 健康检查 配置文件 负载均衡策略 ACL 规则 Web 监控平台 Keepalived 虚拟路由器 核心组件 VRRP ...
- Maven使用WEB-INF/lib下面的jar编译和打包
在某些情况下,maven无法下载依赖的jar,或者依赖的m2会非常的大,上G那是随随便便的事.为了方便修改和编译,在打出的war包基础上,或者直接把tomcat的webapps下的项目拿出来,就可以用 ...
- SAP简介
1. 什么是SAP SAP的英文全名为System Application and Products in Data Processing.SAP既是公司名称,又是其产品的软件名称. 2. SAP的诞 ...
- Centos 安装Jenkins的坎坷
安装Jenkins(简单复制即可) wget -O /etc/yum.repos.d/jenkins.repo http://pkg.jenkins-ci.org/redhat/jenkins.rep ...
- mysql常用知识点之limit
limit函数的应用.limit后面跟整数,如limit 5,表示在结果集中取前5条:limit后跟整数区间,如limit 2,5,表示在结果集中 从第3条开始,取5条数据,第一个整数表示结果集的顺序 ...
- mysql自动生成my.cnf文件
http://imysql.com/my-cnf-wizard.html
- js--事件--事件代理
js--事件--事件代理:https://blog.csdn.net/github_34514750/article/details/53067133
- redis进阶知识
原文地址:https://my.oschina.net/u/4052893/blog/3001173 一.缓存雪崩 1.1什么是缓存雪崩? 回顾一下我们为什么要用缓存(Redis): 现在有个问题,如 ...