BZOJ3316 JC loves Mkk

[Solution]

Binary find the max average value of the candies in float form. Each times you check, return the first segment whose sum is over zero to make the answer a fraction.


[Code]

#include <cstdio>
#include <cmath>
#include <memory.h>
#include <algorithm>

using namespace std;

typedef long long qw;
typedef long double exf;
typedef pair <int, int> dpair;

#ifdef WIN32
#define lld "%I64d"
#else
#define lld "%lld"
#endif

const int maxn = 200009;
const exf eps = 1e-10;

int n, d, u, a[maxn];
int qe[maxn], qo[maxn], eh, et, oh, ot;
exf s[maxn];

void inq(int x) {
	if (x & 1) {
		while (oh < ot && s[x] < s[qo[ot - 1]])
			-- ot;
		qo[ot ++] = x;
	}
	else {
		while (eh < et && s[x] < s[qe[et - 1]])
			-- et;
		qe[et ++] = x;
	}
}

dpair check(exf v) {
	eh = 0, et = 0;
	oh = 0, ot = 0;
	s[0] = 0;
	for (int i = 1; i <= n; ++ i) {
		s[i] = s[i - 1] + a[i] - v;
		if (i & 1) {
			while (i - qo[oh] > u && oh < ot)
				++ oh;
			if (oh < ot && s[i] - s[qo[oh]] >= 0)
				return dpair(qo[oh], i);
		}
		else {
			while (i - qe[eh] > u && eh < et)
				++ eh;
			if (eh < et && s[i] - s[qe[eh]] >= 0)
				return dpair(qe[eh], i);
		}
		if (i - d + 1 >= 0)
			inq(i - d + 1);
	}
	return dpair(-1, -1);
}

inline qw gcd(qw a, qw b) {
	return (b % a) ? gcd(b % a, a) : a;
}

int main() {
#ifndef ONLINE_JUDGE
	freopen("in.txt", "r", stdin);
#endif

	scanf("%d%d%d", &n, &d, &u);
	for (int i = 1; i <= n; ++ i)
		scanf("%d", a + i), a[i + n] = a[i];
	n *= 2;
	exf l = 0, r = 1e9;
	dpair s;
	while (r - l > eps) {
		exf mid = (l + r) / 2.0;
		s = check(mid);
		if (s. first > -1)
			l = mid; 
		else
			r = mid;
	}
	qw t = 0, c;
	s = check(l);
	c = s. second - s. first;
	for (int i = s. first + 1; i <= s. second; ++ i)
		t += a[i];
	qw g = gcd(t, c);
	t /= g;
	c /= g;
	if (c == 1)
		printf(lld "\n", t);
	else
		printf(lld "/" lld "\n", t, c);
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值