제출 #336045

#제출 시각아이디문제언어결과실행 시간메모리
336045aryan12쌀 창고 (IOI11_ricehub)C++17
17 / 100
24 ms3308 KiB
#include <bits/stdc++.h> #include "ricehub.h" using namespace std; int besthub(int r, int l, int x[], long long b) { long long pre[r + 1], suf[r + 1]; pre[0] = 0; pre[1] = 0; for(long long i = 2; i <= r; i++) { pre[i] = pre[i - 1] + (x[i] - x[i - 1]) * (i - 1); } suf[r] = 0; for(long long i = r - 1; i >= 1; i--) { suf[i] = suf[i + 1] + (x[i + 1] - x[i]) * (r - i); } /*for(long long i = 1; i <= r; i++) { cout << pre[i] << " "; } cout << endl; for(long long i = 1; i <= r; i++) { cout << suf[i] << " "; } cout << endl;*/ long long ans = 1; for(long long i = 2; i <= r; i++) { long long left = 1, right = i, best = r; while(left <= right) { long long LeftPoint = (left + right) / 2; long long OptimalField = (LeftPoint + i) / 2; long long cost = pre[OptimalField], cost1 = suf[OptimalField]; cost -= (pre[LeftPoint] + ((x[OptimalField] - x[LeftPoint]) * (LeftPoint - 1))); cost1 -= (suf[i] + ((x[i] - x[OptimalField]) * (r - i))); cost = abs(cost); cost1 = abs(cost1); cost += cost1; /*for(long long j = LeftPoint; j <= i; j++) { cost += abs(x[j] - x[OptimalField]); }*/ //cout << "i = " << i; //cout << ", left = " << left << ", right = " << right << ", LeftPoint = " << LeftPoint << ", Optimal Field = " << OptimalField << endl; if(cost <= b) { right = LeftPoint - 1; best = LeftPoint; } else { left = LeftPoint + 1; } } //cout << i << " " << best << endl; ans = max(ans, i - best + 1); } int lol = ans; return lol; } /*int main() { //cout << "Input the number of rice fields" << endl; int r; cin >> r; //cout << "Input the maximum coordinate" << endl; int l; cin >> l; //cout << "Input r numbers, denoting the rice fields" << endl; int x[r + 1]; for(int i = 1; i <= r; i++) { cin >> x[i]; } //cout << "Input the budget" << endl; int b; cin >> b; cout << besthub(r, l, x, b) << endl; } */
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...