Submission #262444

#TimeUsernameProblemLanguageResultExecution timeMemory
262444c4ts0upRice Hub (IOI11_ricehub)C++17
100 / 100
21 ms3572 KiB
#include "ricehub.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
 
ll n;
vector <ll> arr, costos;

ll Cost(ll s, ll t, ll p) {
	return (p-s)*arr[p] - (costos[p]-costos[s]) + (costos[t+1]-costos[p+1]) - (t-p)*arr[p];
}
 
 
int besthub(int R, int L, int X[], ll B) {
	n = R;
	ll lb = 0, ub = n, mid, p;
	
	for (ll i=0; i<R; i++) arr.push_back((ll)X[i]);
	
	
	// precalculamos los costos
	costos.resize(n+1);
	costos[0] = 0LL;
	for (int i=1; i<=n; i++) costos[i] = costos[i-1] + arr[i-1];
	
	
	// O(log R)
	while (lb <= ub) {
		// sacamos el medio
		mid = (lb+ub)/2;
		
		//cerr << "lb = " << lb << ", ub = " << ub << ", mid = " << mid << endl;
		
		bool success = false;
		
		for (int i=0; i+mid<n; i++) {
			
			p = (2LL*i+mid)/2;
			//cerr << "El costo de [" << i << ", " << i+mid << "] es " << Cost(i, i+mid, p) << endl;
			//cerr << "p = " << p << endl;
			if (Cost(i, i+mid, p) <= B) {
				success = true;
				break;
			}
		}
		
		if (success) lb = mid+1;
		else ub = mid-1;
	}
	//cerr << "lb = " << lb << ", ub = " << ub << ", mid = " << mid << endl;
	return ub+1;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...