Submission #139474

#TimeUsernameProblemLanguageResultExecution timeMemory
139474Sorting쌀 창고 (IOI11_ricehub)C++14
100 / 100
24 ms2680 KiB
#include <bits/stdc++.h>

using namespace std;

const int N = 1e5 + 7;

long long b;
int l, r, *x;
long long prefix[N];

long long calc_sum(int from, int to){
	if(from == 0){
		return prefix[to];
	}
	return prefix[to] - prefix[from - 1];
}

long long find_score(int from, int to){
	long long mid;

	if((to - from) & 1){
		mid = (x[(to + from) / 2] + x[(to + from) / 2 + 1]) / 2;
	}
	else{
		mid = x[(to + from) / 2];
	}

	long long ans = ((to + from) / 2ll - from + 1ll) * mid - calc_sum(from, (to + from) / 2);
	ans += calc_sum((to + from) / 2 + 1, to) - (to - (to + from) / 2ll) * mid;

	return ans;
}

int besthub(int _r, int _l, int *_x, long long _b){
	r = _r;
	l = _l;
	x = _x;
	b = _b;

	int ans = 0;

	prefix[0] = x[0];
	for(int i = 1; i < r; i++){
		prefix[i] = x[i] + prefix[i - 1];
	}
	int j = 0;

	for(int i = 0; i < r; i++){
		for(j = max(i, j); j < r && find_score(i, j) <= b; j++);
		j--;
		ans = max(ans, j - i + 1);
	}

	return ans;
}	
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...