IOI2011 && BZOJ2600 ricehub

BZOJ

分析

直接用两个指针扫,中位数就是放米仓的位置。

代码

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;

template <typename T> inline void read(T &x) {
	T f = 1; x = 0; char c;
    for (c = getchar(); !isdigit(c); c = getchar()) if (c == '-') f = -1;
    for ( ; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48);
    x *= f;
}

int R, ans;
ll L, B, sum;
ll x[100005];

int main() {
	read(R), read(L), read(B);
	for (int i = 1; i <= R; ++i) read(x[i]);
	for (int i = 1, j = 1; j <= R; ++j) {
		sum += x[j] - x[i+j>>1];
		while (sum > B) {
			i++;
			sum -= x[i+j>>1] - x[i-1];
		}
		if (ans < j - i + 1) ans = j - i + 1;
	}
	printf("%d", ans);
	return 0;
}
posted @ 2019-08-30 22:12  小蒟蒻hlw  阅读(109)  评论(0)    收藏  举报