Submission #721284

# Submission time Handle Problem Language Result Execution time Memory
721284 2023-04-10T15:40:21 Z rainboy Radio (Balkan15_RADIO) C
60 / 100
2000 ms 11660 KB
#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 data[N * 3 + 1][5], u_, l_, r_; long long ss[N * 3 + 1];

int node(int i) {
	static int _ = 1;

	data[_][0] = rand_();
	data[_][3] = i;
	data[_][4] = 1, ss[_] = cc[i];
	return _++;
}

void pul(int u) {
	int l = data[u][1], r = data[u][2];

	data[u][4] = data[l][4] + 1 + data[r][4];
	ss[u] = ss[l] + cc[data[u][3]] + ss[r];
}

void split(int u, int i) {
	int c;

	if (u == 0) {
		u_ = l_ = r_ = 0;
		return;
	}
	c = cc[data[u][3]] != cc[i] ? (cc[data[u][3]] < cc[i] ? -1 : 1) : data[u][3] - i;
	if (c < 0) {
		split(data[u][2], i);
		data[u][2] = l_, l_ = u;
	} else if (c > 0) {
		split(data[u][1], i);
		data[u][1] = r_, r_ = u;
	} else {
		u_ = u, l_ = data[u][1], r_ = data[u][2];
		data[u][1] = data[u][2] = 0;
	}
	pul(u);
}

int merge(int u, int v) {
	if (u == 0)
		return v;
	if (v == 0)
		return u;
	if (data[u][0] < data[v][0]) {
		data[u][2] = merge(data[u][2], v), pul(u);
		return u;
	} else {
		data[v][1] = merge(u, data[v][1]), 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[data[u][3]] <= c)
			k += data[u][4] - data[data[u][2]][4], u = data[u][2];
		else
			u = data[u][1];
	return k;
}

long long sum(int u, long long c) {
	long long s;

	s = 0;
	while (u)
		if (cc[data[u][3]] <= c)
			s += ss[u] - ss[data[u][2]], u = data[u][2];
		else
			u = data[u][1];
	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]);
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 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 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 2 ms 340 KB Output is correct
3 Correct 3 ms 340 KB Output is correct
4 Correct 5 ms 340 KB Output is correct
5 Correct 6 ms 400 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 2 ms 332 KB Output is correct
3 Correct 3 ms 340 KB Output is correct
4 Correct 6 ms 368 KB Output is correct
5 Correct 7 ms 340 KB Output is correct
6 Correct 7 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 2 ms 340 KB Output is correct
3 Correct 3 ms 340 KB Output is correct
4 Correct 5 ms 340 KB Output is correct
5 Correct 6 ms 400 KB Output is correct
6 Correct 33 ms 780 KB Output is correct
7 Correct 187 ms 2536 KB Output is correct
8 Correct 470 ms 6012 KB Output is correct
9 Correct 1332 ms 9432 KB Output is correct
10 Correct 802 ms 11588 KB Output is correct
11 Correct 1256 ms 11656 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 2 ms 332 KB Output is correct
3 Correct 3 ms 340 KB Output is correct
4 Correct 6 ms 368 KB Output is correct
5 Correct 7 ms 340 KB Output is correct
6 Correct 7 ms 340 KB Output is correct
7 Correct 40 ms 812 KB Output is correct
8 Correct 257 ms 2940 KB Output is correct
9 Correct 603 ms 5876 KB Output is correct
10 Correct 1310 ms 9108 KB Output is correct
11 Correct 827 ms 7712 KB Output is correct
12 Execution timed out 2036 ms 11660 KB Time limit exceeded
13 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 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 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 2 ms 340 KB Output is correct
10 Correct 3 ms 340 KB Output is correct
11 Correct 5 ms 340 KB Output is correct
12 Correct 6 ms 400 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 2 ms 332 KB Output is correct
15 Correct 3 ms 340 KB Output is correct
16 Correct 6 ms 368 KB Output is correct
17 Correct 7 ms 340 KB Output is correct
18 Correct 7 ms 340 KB Output is correct
19 Correct 33 ms 780 KB Output is correct
20 Correct 187 ms 2536 KB Output is correct
21 Correct 470 ms 6012 KB Output is correct
22 Correct 1332 ms 9432 KB Output is correct
23 Correct 802 ms 11588 KB Output is correct
24 Correct 1256 ms 11656 KB Output is correct
25 Correct 40 ms 812 KB Output is correct
26 Correct 257 ms 2940 KB Output is correct
27 Correct 603 ms 5876 KB Output is correct
28 Correct 1310 ms 9108 KB Output is correct
29 Correct 827 ms 7712 KB Output is correct
30 Execution timed out 2036 ms 11660 KB Time limit exceeded
31 Halted 0 ms 0 KB -