# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
538375 | 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
template<typename T, bool zero_indexing>
struct fenwick_tree{
int N;
vector<T> fen;
fenwick_tree(int n){
N = n, fen.assign(n + 1, 0);
}
void modif(int p, T val){
for(int i = p + zero_indexing;i <= N;i += i & -i)fen[i] += val;
}
T query(int l, int r){
T ret = 0;
for(int i = r + zero_indexing;i;i -= i & -i)ret += fen[i];
for(int i = l-1+zero_indexing;i;i-=i&-i)ret -= fen[i];
return ret;
}
};
const int N = 2e5 + 5;
fenwick_tree<ll, 1> sum(N);
fenwick_tree<int, 1> fen(N);
int k;
map<int,int> m;
ll calc()
{
int x = -1;
int l = 0, r = N - 1;
while(l <= r)
{
int mid = l + r >> 1;
if(fen.query(0, mid) >= (k + 1) / 2)
{
x = mid;
r = mid - 1;
}else l = mid + 1;
}
ll cnt = 0;
if(x)cnt += m[x] * fen.query(0, x - 1) - sum.query(0, x - 1);
cnt += sum.query(x + 1, N - 1) - m[x] * fen.query(x + 1, N - 1);
return cnt;
}
ll besthub(long long n, long long lungime, vector<long long> a, long long buget)
{
vector<int> b(n);
for(int i = 0; i < n; i++){
b[i] = a[i];
}
sort(all(b));
b.erase(unique(all(b)), b.end());
for(int i = 0;i < n;++i){
int c = lower_bound(all(b), a[i]) - b.begin();
m[c] = a[i];
a[i] = c;
}
ll l = 0, r = buget;
int ans = 0;
while(l <= r)
{
ll mid = l+r>>1;
k = mid;
for(int i = 0;i < k;++i){
fen.modif(a[i], 1);
sum.modif(a[i], m[a[i]]);
}
ll ansnow = LLONG_MAX;
ansnow = min(ansnow, max(0LL, calc()));
for(int i = k;i < n;++i){
fen.modif(a[i - k], -1);
fen.modif(a[i], 1);
sum.modif(a[i - k], -m[a[i - k]]);
sum.modif(a[i], m[a[i]]);
ansnow = min(ansnow, max(0LL, calc()));
}
for(int i = n-k; i < n; i++)
{
fen.modif(a[i - k], -1);
sum.modif(a[i - k], -m[a[i - k]]);
}
if(ansnow > buget)
{
r = mid-1;
}
else
{
ans = mid;
l = mid+1;
}
}
return ans;
}
/*
int32_t main(){
startt
int t = 1;
//freopen("perm.in", "r", stdin);
//freopen("perm.out", "w", stdout);
//cin >> t;
while (t--) {
cout << besthub(5, 20, {1, 2, 10, 12, 14}, 6);
}
}
*/