# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1236683 | fauntleroy | Rice Hub (IOI11_ricehub) | C++20 | 0 ms | 0 KiB |
#include <iostream>
#include <cstdio>
#include <vector>
#include <array>
#include <string>
#include <algorithm>
#include <numeric>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <queue>
#include <cmath>
#include <climits>
#include <iomanip>
#include <limits>
#include <tuple>
#include <stack>
#include <bitset>
#include <cstring>
#include <sstream>
#include <functional>
#include <random>
//#include "ricehub.h"
#define ll long long
using namespace std;
int besthub(int R, int L, int x[], ll B) {
int n = R;
vector<ll> d(n - 1);
for (int i = 0; i < n - 1; ++i)
d[i] = x[i + 1] - x[i];
vector<ll> pref(n - 1, 0);
pref[0] = d[0];
for (int i = 1; i < n - 1; ++i)
pref[i] = pref[i - 1] + d[i];
int l = 0, r = n;
int ans = 0;
while (l <= r) {
int k = (l + r) >> 1;
--k;
if (k == 0) {
ans = max(ans, 1);
l = 2;
continue;
}
int l1 = 0, r1 = n - k - 1;
ll mn = 1e9;
for (int i = 0; i < n - k; ++i) {
ll s = pref[i + k - 1] - (i > 0 ? pref[i - 1] : 0);
if (s < mn) {
l1 = i, r1 = i + k - 1;
mn = s;
}
}
int pos = x[l1 + k / 2];
ll d = 0;
for (int i = l1; i <= r1 + 1; ++i)
d += abs(pos - x[i]);
//cout << pos<<' '<<d << '\n';
++k;
if (d > B)
r = k - 1;
else
l = k + 1, ans = max(ans, k);
}
return ans;
}
int main() {
int n;
cin >> n;
int* a = new int[n];
for (int i = 0; i < n; ++i)
cin >> a[i];
int b;
cin >> b;
cout << besthub(n, 0, a, b) << '\n';
}