#include "boxes.h"
#include <bits/stdc++.h>
using namespace std;
#define num long long
vector<num> dp;
struct SegTree {
vector<num> tree;
int size, start, height;
SegTree (int _size) {
size = _size;
height = ceil(log2(size));
start = 1 << height;
tree.resize(start << 1);
}
void update (int index) {
if (index == 0) return ;
if (index < start) {
int n0 = index << 1;
int n1 = n0 + 1;
tree[index] = min(tree[n0], tree[n1]);
}
update(index >> 1);
}
void modify (int index, num value) {
tree[index + start] = value;
update(index + start);
}
num query (int l, int r) {
num res = 1e18;
l += start;
r += start;
while (l < r) {
if ((l & 1) == 1) res = min(res, tree[l]);
if ((r & 1) == 0) res = min(res, tree[r]);
l = (l + 1) >> 1;
r = (r - 1) >> 1;
}
if (l == r) res = min(res, tree[l]);
return res;
}
};
num valuation (int L, int P) {
return min(P, L - P);
}
long long delivery(int N, int K, int L, int p[]) {
sort(p, p + N);
SegTree opt(N + 1);
dp.resize(N + 1, 0);
dp[0] = 0;
opt.modify(0, valuation(L, p[0]) - p[0] + dp[0]);
dp[1] = valuation(L, p[0]) * 2;
opt.modify(1, valuation(L, p[1]) - p[1] + dp[1]);
for (int i = 2; i <= N; i ++) {
int pos = p[i - 1];
num mov = valuation(L, pos) + pos;
num mmv = opt.query(max(0, i - K), i - 1);
dp[i] = mmv + mov;
opt.modify(i, valuation(L, p[i]) - p[i] + dp[i]);
}
//for (int i = 0; i <= N; i ++)
// cout << dp[i] << " ";
//cout << endl;
return dp[N];
}
Compilation message
boxes.cpp: In constructor 'SegTree::SegTree(int)':
boxes.cpp:16:22: warning: conversion from 'double' to 'int' may change value [-Wfloat-conversion]
16 | height = ceil(log2(size));
| ~~~~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 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 |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 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 |
1 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 |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 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 |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
0 ms |
212 KB |
Output is correct |
22 |
Correct |
0 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
1 ms |
212 KB |
Output is correct |
27 |
Correct |
1 ms |
212 KB |
Output is correct |
28 |
Correct |
1 ms |
212 KB |
Output is correct |
29 |
Correct |
1 ms |
212 KB |
Output is correct |
30 |
Correct |
1 ms |
212 KB |
Output is correct |
31 |
Correct |
1 ms |
212 KB |
Output is correct |
32 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 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 |
1 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 |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 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 |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
0 ms |
212 KB |
Output is correct |
22 |
Correct |
0 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
1 ms |
212 KB |
Output is correct |
27 |
Correct |
1 ms |
212 KB |
Output is correct |
28 |
Correct |
1 ms |
212 KB |
Output is correct |
29 |
Correct |
1 ms |
212 KB |
Output is correct |
30 |
Correct |
1 ms |
212 KB |
Output is correct |
31 |
Correct |
1 ms |
212 KB |
Output is correct |
32 |
Correct |
1 ms |
212 KB |
Output is correct |
33 |
Correct |
157 ms |
28352 KB |
Output is correct |
34 |
Correct |
119 ms |
30336 KB |
Output is correct |
35 |
Correct |
96 ms |
30900 KB |
Output is correct |
36 |
Correct |
147 ms |
38120 KB |
Output is correct |
37 |
Correct |
146 ms |
38268 KB |
Output is correct |
38 |
Correct |
145 ms |
38172 KB |
Output is correct |
39 |
Correct |
145 ms |
36680 KB |
Output is correct |
40 |
Correct |
126 ms |
32080 KB |
Output is correct |
41 |
Correct |
151 ms |
38340 KB |
Output is correct |
42 |
Correct |
123 ms |
32368 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 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 |
1 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 |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 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 |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
0 ms |
212 KB |
Output is correct |
22 |
Correct |
0 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
1 ms |
212 KB |
Output is correct |
27 |
Correct |
1 ms |
212 KB |
Output is correct |
28 |
Correct |
1 ms |
212 KB |
Output is correct |
29 |
Correct |
1 ms |
212 KB |
Output is correct |
30 |
Correct |
1 ms |
212 KB |
Output is correct |
31 |
Correct |
1 ms |
212 KB |
Output is correct |
32 |
Correct |
1 ms |
212 KB |
Output is correct |
33 |
Correct |
157 ms |
28352 KB |
Output is correct |
34 |
Correct |
119 ms |
30336 KB |
Output is correct |
35 |
Correct |
96 ms |
30900 KB |
Output is correct |
36 |
Correct |
147 ms |
38120 KB |
Output is correct |
37 |
Correct |
146 ms |
38268 KB |
Output is correct |
38 |
Correct |
145 ms |
38172 KB |
Output is correct |
39 |
Correct |
145 ms |
36680 KB |
Output is correct |
40 |
Correct |
126 ms |
32080 KB |
Output is correct |
41 |
Correct |
151 ms |
38340 KB |
Output is correct |
42 |
Correct |
123 ms |
32368 KB |
Output is correct |
43 |
Correct |
1885 ms |
477040 KB |
Output is correct |
44 |
Correct |
1484 ms |
399984 KB |
Output is correct |
45 |
Correct |
1002 ms |
407908 KB |
Output is correct |
46 |
Correct |
1816 ms |
477976 KB |
Output is correct |
47 |
Correct |
1829 ms |
477880 KB |
Output is correct |
48 |
Correct |
1662 ms |
476780 KB |
Output is correct |
49 |
Correct |
1815 ms |
462916 KB |
Output is correct |
50 |
Correct |
1593 ms |
416836 KB |
Output is correct |
51 |
Correct |
1989 ms |
477816 KB |
Output is correct |
52 |
Correct |
1514 ms |
419428 KB |
Output is correct |