首页 技术 正文
技术 2022年11月8日
0 收藏 328 点赞 1,797 浏览 1300 个字

题目传送门

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_backtemplate<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;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,085
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,560
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,409
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,182
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,819
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,902