社区讨论
不太会用单调队列,求指点!
P1419寻找段落参与者 2已保存回复 1
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 1 条
- 当前快照
- 1 份
- 快照标识符
- @mhju5np8
- 此快照首次捕获于
- 2025/11/04 08:33 4 个月前
- 此快照最后确认于
- 2025/11/04 08:33 4 个月前
CPP
#include<cstdio>
#include<queue>
#include<algorithm>
using namespace std;
const int maxn = 1e5 + 5;
int n, s, t, num[maxn];
int minx = 10000, maxx = -10000;
void Input() {
scanf("%d %d %d", &n, &s, &t);
for (int i = 1; i <= n; ++i) {
scanf("%d", &num[i]);
minx = min(minx, num[i]);
maxx = max(maxx, num[i]);
}
}
bool Check(double u) {
double sum[maxn];
for (int i = 1; i <= n; ++i) {
sum[i] = sum[i - 1] + num[i] - u;
}
deque<int> q;
for (int i = 1; i <= n; ++i) {
while (!q.empty() && sum[q.back()] > sum[i]) {
q.pop_back();
}
q.push_back(i);
while (!q.empty() && q.front() < i - t) {
q.pop_front();
}
if (i >= s && sum[i] - sum[q.front()] > 0) {
return true;
}
}
return 0;
}
double ans;
void Binary_Search() {
double l = minx, r = maxx;
while (r - l > 0.0001) {
double mid = (l + r) / 2;
if (Check(mid) == true) {
l = mid;
ans = mid;
} else {
r = mid;
}
}
}
int main() {
Input();
Binary_Search();
printf("%.3lf", ans);
return 0;
}
回复
共 1 条回复,欢迎继续交流。
正在加载回复...