## Submission #979916

# Submission time Handle Problem Language Result Execution time Memory
979916 2024-05-11T15:57:53 Z SmuggingSpun Rice Hub (IOI11_ricehub) C++14
100 / 100
221 ms 8528 KB
#include "ricehub.h"
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
template<class T>void maximize(T& a, T b){
if(a < b){
a = b;
}
}
const int lim = 1e5 + 5;
int N = 1, ans = 0, cnt[lim], to_X[lim];
map<int, int>X_to;
ll sum[lim];
ll get_value(int p, int range){
int P = lower_bound(to_X + 1, to_X + N + 1, to_X[p] - range) - to_X - 1;
ll sum_left = 1LL * to_X[p] * (cnt[p] - cnt[P]) - sum[p] + sum[P];
P = upper_bound(to_X + 1, to_X + N + 2, to_X[p] + range) - to_X - 1;
return sum_left + sum[P] - sum[p] - 1LL * to_X[p] * (cnt[P] - cnt[p]);
}
int besthub(int n, int L, int X[], ll B){
memset(cnt, 0, sizeof(cnt));
memset(sum, 0, sizeof(sum));
X_to[to_X[1] = X[0]] = 1;
for(int i = 0; i < n; i++){
if(i > 0 && X[i] != X[i - 1]){
X_to[to_X[++N] = X[i]] = N;
}
cnt[N]++;
sum[N] += X[i];
}
for(int i = 1; i <= N; i++){
cnt[i] += cnt[i - 1];
sum[i] += sum[i - 1];
}
to_X[N + 1] = INT_MAX;
for(int i = 1; i <= N; i++){
int low = 0, high = L, range;
while(low <= high){
int mid = (low + high) >> 1;
if(get_value(i, mid) <= B){
low = (range = mid) + 1;
}
else{
high = mid - 1;
}
}
ll remain = B - get_value(i, range);
map<int, int>::iterator it = X_to.find(to_X[i] - range - 1);
if(it != X_to.end()){
int can_add = min(ll(cnt[it->second] - cnt[it->second - 1]), remain / (range + 1));
remain -= 1LL * can_add * (range + 1);
}
it = X_to.find(to_X[i] + range + 1);
if(it != X_to.end()){
add_cnt += min(ll(cnt[it->second] - cnt[it->second - 1]), remain / (range + 1));
}
maximize(ans, add_cnt + cnt[upper_bound(to_X + 1, to_X + N + 2, to_X[i] + range) - to_X - 1] - cnt[lower_bound(to_X + 1, to_X + N + 1, to_X[i] - range) - to_X - 1]);
}
return ans;
}

### Compilation message

ricehub.cpp: In function 'int besthub(int, int, int*, ll)':
ricehub.cpp:26:14: warning: operation on 'N' may be undefined [-Wsequence-point]
26 |    X_to[to_X[++N] = X[i]] = N;
|              ^~~
ricehub.cpp:26:14: warning: operation on 'N' may be undefined [-Wsequence-point]
ricehub.cpp:48:50: warning: 'range' may be used uninitialized in this function [-Wmaybe-uninitialized]
48 |   map<int, int>::iterator it = X_to.find(to_X[i] - range - 1);
|                                          ~~~~~~~~^~~~~~~

#### Subtask #1 17.0 / 17.0

# Verdict Execution time Memory Grader output
1 Correct 1 ms 2652 KB Output is correct
2 Correct 1 ms 2652 KB Output is correct
3 Correct 1 ms 2652 KB Output is correct
4 Correct 1 ms 2652 KB Output is correct
5 Correct 1 ms 2480 KB Output is correct

#### Subtask #2 25.0 / 25.0

# Verdict Execution time Memory Grader output
1 Correct 1 ms 2652 KB Output is correct
2 Correct 1 ms 2652 KB Output is correct
3 Correct 1 ms 2652 KB Output is correct
4 Correct 1 ms 2652 KB Output is correct
5 Correct 1 ms 2652 KB Output is correct
6 Correct 1 ms 2648 KB Output is correct
7 Correct 1 ms 2648 KB Output is correct
8 Correct 2 ms 2652 KB Output is correct
9 Correct 1 ms 2652 KB Output is correct
10 Correct 1 ms 2652 KB Output is correct
11 Correct 1 ms 2652 KB Output is correct
12 Correct 1 ms 2652 KB Output is correct
13 Correct 1 ms 2652 KB Output is correct
14 Correct 1 ms 2652 KB Output is correct
15 Correct 1 ms 2480 KB Output is correct
16 Correct 1 ms 2652 KB Output is correct
17 Correct 1 ms 2652 KB Output is correct
18 Correct 1 ms 2652 KB Output is correct
19 Correct 1 ms 2652 KB Output is correct
20 Correct 1 ms 2652 KB Output is correct
21 Correct 1 ms 2652 KB Output is correct
22 Correct 1 ms 2652 KB Output is correct
23 Correct 1 ms 2652 KB Output is correct
24 Correct 1 ms 2648 KB Output is correct
25 Correct 1 ms 2652 KB Output is correct
26 Correct 1 ms 2652 KB Output is correct
27 Correct 1 ms 2652 KB Output is correct
28 Correct 1 ms 2648 KB Output is correct

#### Subtask #3 26.0 / 26.0

# Verdict Execution time Memory Grader output
1 Correct 1 ms 2652 KB Output is correct
2 Correct 1 ms 2652 KB Output is correct
3 Correct 1 ms 2652 KB Output is correct
4 Correct 2 ms 2652 KB Output is correct
5 Correct 1 ms 2648 KB Output is correct
6 Correct 2 ms 2652 KB Output is correct
7 Correct 1 ms 2652 KB Output is correct
8 Correct 1 ms 2652 KB Output is correct
9 Correct 1 ms 2476 KB Output is correct
10 Correct 1 ms 2904 KB Output is correct
11 Correct 1 ms 2652 KB Output is correct
12 Correct 1 ms 2652 KB Output is correct
13 Correct 1 ms 2652 KB Output is correct
14 Correct 1 ms 2508 KB Output is correct
15 Correct 1 ms 2492 KB Output is correct
16 Correct 1 ms 2652 KB Output is correct
17 Correct 1 ms 2652 KB Output is correct
18 Correct 1 ms 2696 KB Output is correct
19 Correct 1 ms 2652 KB Output is correct
20 Correct 2 ms 2652 KB Output is correct
21 Correct 1 ms 2512 KB Output is correct
22 Correct 1 ms 2652 KB Output is correct
23 Correct 3 ms 2652 KB Output is correct
24 Correct 5 ms 2652 KB Output is correct
25 Correct 7 ms 2784 KB Output is correct
26 Correct 6 ms 2908 KB Output is correct
27 Correct 9 ms 2908 KB Output is correct
28 Correct 8 ms 2908 KB Output is correct

#### Subtask #4 32.0 / 32.0

# Verdict Execution time Memory Grader output
1 Correct 43 ms 3376 KB Output is correct
2 Correct 38 ms 3420 KB Output is correct
3 Correct 144 ms 7344 KB Output is correct
4 Correct 221 ms 7340 KB Output is correct
5 Correct 9 ms 2652 KB Output is correct
6 Correct 5 ms 2648 KB Output is correct
7 Correct 14 ms 3672 KB Output is correct
8 Correct 13 ms 3676 KB Output is correct
9 Correct 19 ms 3384 KB Output is correct
10 Correct 18 ms 3164 KB Output is correct
11 Correct 141 ms 8400 KB Output is correct
12 Correct 200 ms 8528 KB Output is correct
13 Correct 99 ms 5212 KB Output is correct
14 Correct 95 ms 5212 KB Output is correct
15 Correct 105 ms 6916 KB Output is correct
16 Correct 145 ms 6912 KB Output is correct
17 Correct 127 ms 7760 KB Output is correct
18 Correct 172 ms 7764 KB Output is correct
19 Correct 133 ms 8100 KB Output is correct
20 Correct 197 ms 8088 KB Output is correct