#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;
}
int besthub(int n, int lungime, int 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);
}
}
*/
Compilation message
ricehub.cpp: In member function 'void prefix_sum::build(std::vector<int>)':
ricehub.cpp:22:667: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
22 | 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
| ~~^~~~~~~~~~
ricehub.cpp: In function 'll calc()':
ricehub.cpp:52:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
52 | int mid = l + r >> 1;
| ~~^~~
ricehub.cpp: In function 'int besthub(int, int, int*, long long int)':
ricehub.cpp:85:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
85 | ll mid = l+r>>1;
| ~^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2668 KB |
Output is correct |
2 |
Incorrect |
1 ms |
2644 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Incorrect |
2 ms |
2644 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
5 ms |
5280 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
142 ms |
3880 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |