#include <stdio.h>
#define N 100000
#define X 1000000000
#define INF 0x3f3f3f3f3f3f3f3fLL
long long min(long long a, long long b) { return a < b ? a : b; }
unsigned int Z = 12345;
int rand_() {
return (Z *= 3) >> 1;
}
int xx[N * 2], cc[N * 3];
void sort(int *ii, int l, int r) {
while (l < r) {
int i = l, j = l, k = r, i_ = ii[l + rand_() % (r - l)], tmp;
while (j < k)
if (xx[ii[j]] == xx[i_])
j++;
else if (xx[ii[j]] < xx[i_]) {
tmp = ii[i], ii[i] = ii[j], ii[j] = tmp;
i++, j++;
} else {
k--;
tmp = ii[j], ii[j] = ii[k], ii[k] = tmp;
}
sort(ii, l, i);
l = k;
}
}
int zz[N * 3 + 1], ll[N * 3 + 1], rr[N * 3 + 1], ii_[N * 3 + 1], sz[N * 3 + 1], u_, l_, r_; long long ss[N * 3 + 1];
int node(int i) {
static int _ = 1;
zz[_] = rand_();
ii_[_] = i;
sz[_] = 1, ss[_] = cc[i];
return _++;
}
void pul(int u) {
int l = ll[u], r = rr[u];
sz[u] = sz[l] + 1 + sz[r];
ss[u] = ss[l] + cc[ii_[u]] + ss[r];
}
void split(int u, int i) {
int c;
if (u == 0) {
u_ = l_ = r_ = 0;
return;
}
c = cc[ii_[u]] != cc[i] ? (cc[ii_[u]] < cc[i] ? -1 : 1) : ii_[u] - i;
if (c < 0) {
split(rr[u], i);
rr[u] = l_, l_ = u;
} else if (c > 0) {
split(ll[u], i);
ll[u] = r_, r_ = u;
} else {
u_ = u, l_ = ll[u], r_ = rr[u];
ll[u] = rr[u] = 0;
}
pul(u);
}
int merge(int u, int v) {
if (u == 0)
return v;
if (v == 0)
return u;
if (zz[u] < zz[v]) {
rr[u] = merge(rr[u], v), pul(u);
return u;
} else {
ll[v] = merge(u, ll[v]), pul(v);
return v;
}
}
int tr_add(int u, int i) {
split(u, i);
return merge(merge(l_, node(i)), r_);
}
int tr_remove(int u, int i) {
split(u, i);
return merge(l_, r_);
}
int count(int u, long long c) {
int k;
k = 0;
while (u)
if (cc[ii_[u]] <= c)
k += sz[u] - sz[rr[u]], u = rr[u];
else
u = ll[u];
return k;
}
long long sum(int u, long long c) {
long long s;
s = 0;
while (u)
if (cc[ii_[u]] <= c)
s += ss[u] - ss[rr[u]], u = rr[u];
else
u = ll[u];
return s;
}
int main() {
static int ww[N], ii[N * 2];
int n, k, h, i, i_, ul, um, ur, x, kl, km, kr;
long long w, lower, upper, c, ans;
scanf("%d%d", &n, &k);
w = 0;
for (i = 0; i < n; i++) {
int p;
scanf("%d%d%d", &x, &p, &ww[i]);
w += ww[i];
xx[i << 1 | 0] = x - p, xx[i << 1 | 1] = x + p;
cc[i * 3 + 0] = ww[i] + xx[i << 1 | 0];
cc[i * 3 + 1] = ww[i];
cc[i * 3 + 2] = ww[i] - xx[i << 1 | 1];
}
for (i = 0; i < n * 2; i++)
ii[i] = i;
sort(ii, 0, n * 2);
ul = 0, um = 0, ur = 0;
for (i = 0; i < n; i++)
ur = tr_add(ur, i * 3 + 0);
ans = INF;
for (h = 0; h < n * 2; h++) {
i_ = ii[h], i = i_ >> 1, x = xx[i_];
if ((i_ & 1) == 0) {
ur = tr_remove(ur, i * 3 + 0);
um = tr_add(um, i * 3 + 1);
} else {
um = tr_remove(um, i * 3 + 1);
ul = tr_add(ul, i * 3 + 2);
}
lower = -1, upper = (long long) X * 4 + 1;
while (upper - lower > 1) {
c = (lower + upper) / 2;
if (count(ul, c - x) + count(um, c) + count(ur, c + x) <= k)
lower = c;
else
upper = c;
}
c = lower;
kl = count(ul, c - x), km = count(um, c), kr = count(ur, c + x);
ans = min(ans, sum(ul, c - x) + (long long) x * kl + sum(um, c) + sum(ur, c + x) - (long long) x * kr + (c + 1) * (k - kl - km - kr) - w);
}
printf("%lld\n", ans);
return 0;
}
Compilation message
radio.c: In function 'main':
radio.c:128:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
128 | scanf("%d%d", &n, &k);
| ^~~~~~~~~~~~~~~~~~~~~
radio.c:133:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
133 | scanf("%d%d%d", &x, &p, &ww[i]);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 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 |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
292 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
288 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
2 ms |
340 KB |
Output is correct |
4 |
Correct |
3 ms |
412 KB |
Output is correct |
5 |
Correct |
6 ms |
460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
2 ms |
340 KB |
Output is correct |
3 |
Correct |
3 ms |
296 KB |
Output is correct |
4 |
Correct |
4 ms |
432 KB |
Output is correct |
5 |
Correct |
5 ms |
416 KB |
Output is correct |
6 |
Correct |
6 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
288 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
2 ms |
340 KB |
Output is correct |
4 |
Correct |
3 ms |
412 KB |
Output is correct |
5 |
Correct |
6 ms |
460 KB |
Output is correct |
6 |
Correct |
32 ms |
1028 KB |
Output is correct |
7 |
Correct |
173 ms |
3016 KB |
Output is correct |
8 |
Correct |
376 ms |
7104 KB |
Output is correct |
9 |
Correct |
1181 ms |
11688 KB |
Output is correct |
10 |
Correct |
647 ms |
13020 KB |
Output is correct |
11 |
Correct |
1283 ms |
14752 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
2 ms |
340 KB |
Output is correct |
3 |
Correct |
3 ms |
296 KB |
Output is correct |
4 |
Correct |
4 ms |
432 KB |
Output is correct |
5 |
Correct |
5 ms |
416 KB |
Output is correct |
6 |
Correct |
6 ms |
468 KB |
Output is correct |
7 |
Correct |
42 ms |
964 KB |
Output is correct |
8 |
Correct |
186 ms |
3288 KB |
Output is correct |
9 |
Correct |
550 ms |
6796 KB |
Output is correct |
10 |
Correct |
1099 ms |
10452 KB |
Output is correct |
11 |
Correct |
633 ms |
9000 KB |
Output is correct |
12 |
Correct |
1834 ms |
13800 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 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 |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
292 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
288 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
2 ms |
340 KB |
Output is correct |
11 |
Correct |
3 ms |
412 KB |
Output is correct |
12 |
Correct |
6 ms |
460 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
2 ms |
340 KB |
Output is correct |
15 |
Correct |
3 ms |
296 KB |
Output is correct |
16 |
Correct |
4 ms |
432 KB |
Output is correct |
17 |
Correct |
5 ms |
416 KB |
Output is correct |
18 |
Correct |
6 ms |
468 KB |
Output is correct |
19 |
Correct |
32 ms |
1028 KB |
Output is correct |
20 |
Correct |
173 ms |
3016 KB |
Output is correct |
21 |
Correct |
376 ms |
7104 KB |
Output is correct |
22 |
Correct |
1181 ms |
11688 KB |
Output is correct |
23 |
Correct |
647 ms |
13020 KB |
Output is correct |
24 |
Correct |
1283 ms |
14752 KB |
Output is correct |
25 |
Correct |
42 ms |
964 KB |
Output is correct |
26 |
Correct |
186 ms |
3288 KB |
Output is correct |
27 |
Correct |
550 ms |
6796 KB |
Output is correct |
28 |
Correct |
1099 ms |
10452 KB |
Output is correct |
29 |
Correct |
633 ms |
9000 KB |
Output is correct |
30 |
Correct |
1834 ms |
13800 KB |
Output is correct |
31 |
Correct |
0 ms |
340 KB |
Output is correct |
32 |
Correct |
490 ms |
4336 KB |
Output is correct |
33 |
Correct |
386 ms |
6164 KB |
Output is correct |
34 |
Correct |
1043 ms |
7480 KB |
Output is correct |
35 |
Correct |
686 ms |
9004 KB |
Output is correct |
36 |
Execution timed out |
2041 ms |
13668 KB |
Time limit exceeded |
37 |
Halted |
0 ms |
0 KB |
- |