Submission #787064

# Submission time Handle Problem Language Result Execution time Memory
787064 2023-07-18T17:58:50 Z allin27x Rice Hub (IOI11_ricehub) C++17
100 / 100
217 ms 13032 KB
#include <bits/stdc++.h>
using namespace std;
#define int long long
 
int val[(int) 1e5+4];
unordered_map<int, int> key;
 
vector<int> a;
int n;

struct SGT{
	vector<int> t;
	int n;
	int ofs;
	SGT(vector<int> ar, bool indexed = false){
		n = ar.size();
		ofs = indexed?(1ll<<(int)(ceil(log2(n+1)))) : n;
		t.resize(2*ofs, 0);
		for (int i=0; i<n; i++) t[ofs+i] = ar[i];
		for (int i=ofs-1; i>0; i--) t[i] = t[i<<1] + t[i<<1|1];
 
	}
	void update(int p, int inc){
		for (t[p+=ofs]+=inc; p>1; p>>=1) t[p>>1] = t[p] + t[p^1];
	}
	int query(int l, int r){
		int res = 0;
		for (l+=ofs,r+=ofs+1; l<r; l>>=1, r>>=1){
			if (l&1) res += t[l++];
			if (r&1) res += t[--r];
		}
		return res;
	}
	int median(){
		int p=1;
		int m = (t[1]+1)/2;
		for (; p<ofs;) 
			if (t[p<<1]<m) m-=t[p<<1], p =p<<1|1;
			else p = p<<1;
		return p-ofs;
	}
};
 
int calc(SGT &freq, SGT &sum){
	int m = freq.median();
	return val[m]*freq.query(0,m)-sum.query(0,m) + sum.query(m,sum.n-1) - val[m]*freq.query(m, freq.n-1);
}
 
int check(int k, int B){
	vector<int> b (n, 0);
	SGT freq = SGT(b, 1);
	SGT sum  = SGT(b, 0);
	for (int i=0; i<k; i++) freq.update(key[a[i]], 1), sum.update(key[a[i]], a[i]);	
	if (calc(freq, sum)<=B) return 1;
	for (int r = k; r<n; r++){
		freq.update(key[a[r]],1); freq.update(key[a[r-k]], -1);
		sum.update(key[a[r]], a[r]); sum.update(key[a[r-k]], -a[r-k]);
		if (calc(freq, sum)<=B) return 1;
	}
	return 0;
}

signed besthub(signed R, signed L, signed X[], int B){	
	n = R;
	int ind = 0, last = -1;
	for (int i=0; i<n; i++){
		if (X[i] == last) continue;
		last = X[i];
		val[ind] = X[i];
		key[X[i]] = ind++;
	}
	a.resize(R);
	for (int i=0; i<R; i++) a[i] = X[i];

	int l = 0, r = R;
	while (l<r){
		int m = (l+r+1)/2;
		if (check(m,B)){
			l = m;
		} else {
			r = m - 1;
		}
	}
	return l;
}

// signed main(){
// 	signed r= 5; signed l = 20; 
// 	signed x[] = {1,2,10,12,14};
// 	int b = 6;
// 	cout<<best_hub(r, l , x, b);
// }
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 1 ms 212 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 2 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 5 ms 672 KB Output is correct
22 Correct 5 ms 672 KB Output is correct
23 Correct 4 ms 852 KB Output is correct
24 Correct 4 ms 852 KB Output is correct
25 Correct 5 ms 896 KB Output is correct
26 Correct 5 ms 932 KB Output is correct
27 Correct 10 ms 928 KB Output is correct
28 Correct 10 ms 936 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 74 ms 2656 KB Output is correct
2 Correct 70 ms 2612 KB Output is correct
3 Correct 126 ms 11956 KB Output is correct
4 Correct 131 ms 11960 KB Output is correct
5 Correct 100 ms 3896 KB Output is correct
6 Correct 104 ms 3996 KB Output is correct
7 Correct 105 ms 7688 KB Output is correct
8 Correct 113 ms 7696 KB Output is correct
9 Correct 104 ms 4256 KB Output is correct
10 Correct 113 ms 4236 KB Output is correct
11 Correct 196 ms 13004 KB Output is correct
12 Correct 162 ms 13032 KB Output is correct
13 Correct 216 ms 6476 KB Output is correct
14 Correct 217 ms 6452 KB Output is correct
15 Correct 123 ms 9956 KB Output is correct
16 Correct 121 ms 9964 KB Output is correct
17 Correct 160 ms 12000 KB Output is correct
18 Correct 162 ms 12060 KB Output is correct
19 Correct 152 ms 12528 KB Output is correct
20 Correct 155 ms 12560 KB Output is correct