洛谷第1419题,https://www.luogu.com.cn/problem/P1419我看了题解以后只是把题解中数组实现的队列换成stl库里面的deque,为什么换了以后测试点变成一个没过了。
题解是这样的
#include <iostream>
#include <cstdio>
#include <cstdlib>
#define maxn 100010
using namespace std;
int n, s, t, i;
double l, r, mid, ans;
double a[maxn];
int b[maxn];
double sum[maxn];
int q[maxn];
bool check(double x) {
int i, l = 1, r = 0;
for (i = 1; i <= n; i++)
a[i] = (double)b[i] - x;
sum[0] = 0;
for (i = 1; i <= n; i++)
sum[i] = sum[i - 1] + a[i];
for (i = 1; i <= n; i++) {
if (i >= s) {
while (r >= l && sum[i - s] < sum[q[r]]) r--;
q[++r] = i - s;
}
if (l <= r && q[l] < i - t) l++;
if (l <= r && sum[i] - sum[q[l]] >= 0) return true;
}//检查区间和,即0到i的和减去0到q[l]的和,q里面是下标。
return false;
}
int main() {
scanf("%d", &n);
scanf("%d%d", &s, &t);
for (i = 1; i <= n; i++)
scanf("%d", &b[i]);
ans = l = -10000; r = 10000;
while (r - l > 1e-5) {
mid = (l + r) / 2;
if (check(mid))
ans = l = mid;
else r = mid;
}
printf("%.3lf\n", ans);
return 0;
}
改了以后一个没过:
#include <iostream>
#include <deque>
#include <cstdio>
#define maxn 100010
using namespace std;
int n, s, t, i;
double l, r, mid, ans;
double a[maxn];
int b[maxn];
double sum[maxn];
deque<int> q;
bool check(double x) {
int i, l = 1, r = 0;
for (i = 1; i <= n; i++)
a[i] = (double)b[i] - x;
sum[0] = 0;
for (i = 1; i <= n; i++)
sum[i] = sum[i - 1] + a[i];
for (i = 1; i <= n; i++) {
if (i >= s) {
while (!q.empty() && sum[i - s] < sum[q.back()]) {
q.pop_back();
}
q.push_back(i - s);
}
if (!q.empty() && q.front() < i - t) {
q.pop_front();
}
if (!q.empty() && sum[i] - sum[q.front()] >= 0) {
return true;
}
}
return false;
}
int main() {
scanf("%d", &n);
scanf("%d%d", &s, &t);
for (i = 1; i <= n; i++)
scanf("%d", &b[i]);
ans = l = -10000;
r = 10000;
while (r - l > 1e-5) {
mid = (l + r) / 2;
if (check(mid))
ans = l = mid;
else
r = mid;
}
printf("%.3lf\n", ans);
return 0;
}