bzoj5090 [Lydsy1711月赛]组题 分数规划
题目传送门
https://lydsy.com/JudgeOnline/problem.php?id=5090
题解
令 \(s_i\) 表示 \(a_i\) 的前缀和。
那么平均难度系数就是
\[\frac{s_r - s_{l-1}}{r-(l-1)}
\]
于是直接分数规划。二分以后是这个样子的:
\[(s_r - k\cdot r) - (s_l - k \cdot l) \geq 0
\]
这个很好求。
但是问题在于答案需要以分数形式输出。但是二分的时候只能二分出来浮点数。
怎么办呢?
实际上我们可以把二分出来的答案对应的区间给重新用分数计算一遍答案。
时间复杂度 \(O(n\log A)\)。
#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, k, ansl, ansr, maxa, mina, posi;
ll a[N];
int p[N];
double b[N], c[N];
inline bool check(double mid) {
for (int i = 1; i <= n; ++i) b[i] = a[i] - mid * i, c[i] = std::min(c[i - 1], b[i]), p[i] = c[i] == b[i] ? i : p[i - 1];
double ans = mina;
for (int i = k; i <= n; ++i) smax(ans, b[i] - c[i - k]) && (ansl = p[i - k] + 1, ansr = i);
// dbg("mid = %lf, ans = %lf, ansl = %d, ansr = %d\n", mid, ans, ansl, ansr);
return ans >= 0;
}
inline void work() {
double l = mina, r = maxa;
while (r - l >= 0.1) {
double mid = (l + r) / 2;
if (check(mid)) l = mid;
else r = mid;
}
ll sum = a[ansr] - a[ansl - 1], tmp;
posi = sum >= 0, sum = std::abs(sum);
tmp = std::__gcd(sum, ansr - ansl + 1ll);
// dbg("ansl = %d, ansr = %d\n", ansl, ansr);
if (!posi) putchar('-');
printf("%lld/%lld\n", sum / tmp, (ansr - ansl + 1) / tmp);
}
inline void init() {
read(n), read(k);
for (int i = 1; i <= n; ++i) read(a[i]), smax(maxa, a[i]), smin(mina, a[i]), a[i] += a[i - 1];
}
int main() {
#ifdef hzhkk
freopen("hkk.in", "r", stdin);
#endif
init();
work();
fclose(stdin), fclose(stdout);
return 0;
}