# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
538586 | ssense | Rice Hub (IOI11_ricehub) | C++14 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#include "ricehub.h"
#define startt ios_base::sync_with_stdio(false);cin.tie(0);
typedef long long ll;
using namespace std;
#define vint vector<int>
#define all(v) v.begin(), v.end()
#define MOD 1000000007
#define MOD2 998244353
#define MX 1000000000
#define MXL 1000000000000000000
#define PI (ld)2*acos(0.0)
#define nax 200005
#define pb push_back
#define sc second
#define fr first
#define int long long
#define endl '\n'
#define ld long double
#define NO cout << "NO" << endl
#define YES cout << "YES" << endl
int ceildiv(int one, int two) {if (one % two == 0) {return one / two;}else {return one / two + 1;}} int power(int n, int pow, int m) {if (pow == 0) return 1;if (pow % 2 == 0) {ll x = power(n, pow / 2, m);return (x * x) % m;}else return (power(n, pow - 1, m) * n) % m;} int gcd(int a, int b) { if (!b)return a; return gcd(b, a % b);} int factorial(int n, int mod) {if (n > 1)return (n * factorial(n - 1, mod)) % mod; else return 1;} int lcm(int a, int b) {return (a * b) / gcd(a, b);} vector<int> read(int n) {vector<int> a; for (int i = 0; i < n; i++) { int x; cin >> x; a.pb(x);} return a;}struct prefix_sum{vint pref;void build(vint a){pref.pb(0);for(int i = 0; i < a.size(); i++){pref.pb(pref.back()+a[i]);}}int get(int l, int r){return pref[r]-pref[l-1];}};//ssense
int besthub(int n, int lungime, vector<int> a, long long buget)
{
vector<int> prefix;
for(int i = 0; i < n; i++)
{
if(i == 0)
{
prefix.pb(a[i]);
}
else
{
prefix.pb(a[i]+prefix.back());
}
}
vector<int> suffix;
for(int i = n-1; i >= 0; i--)
{
if(i == n-1)
{
suffix.pb(lungime-a[i]);
}
else
{
suffix.pb(suffix.back()+lungime-a[i]);
}
}
reverse(all(suffix));
int ans = 0;
int l = 1, r = n;
while(l <= r)
{
int mid = l+r>>1;
deque<int> q;
for(int i = 0; i < mid; i++)
{
q.push_back(i);
}
int now = suffix[q.front()]-a[q[mid/2]]*(mid-1-mid/2)+prefix[q.back()]-a[mid/2]*(mid/2);
ll ansnow = now;
for(int i = mid; i < n; i++)
{
q.pop_front();
q.push_back(a[i]);
now = suffix[q.front()]-a[q[mid/2]]*(mid-1-mid/2)+prefix[q.back()]-a[mid/2]*(mid/2);
ansnow = min(now, ansnow);
}
if(ansnow > buget)
{
r = mid-1;
}
else
{
ans = mid;
l = mid+1;
}
}
return ans;
}
/*
int32_t main(){
startt
int t = 1;
//cin >> t;
while (t--) {
int a[5]= {1, 2, 10, 12, 14};
cout << besthub(5, 20, a, 6);
}
}
*/