답안 #721325

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
721325 2023-04-10T16:55:52 Z rainboy Radio (Balkan15_RADIO) C
100 / 100
274 ms 8960 KB
#include <stdio.h>

#define N	100000
#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], n, k;

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 cc[6][N], iq[6][N + 1], pq[N], tt[N], cnt[6], t;

int lt(int i, int j) { return cc[t][i] < cc[t][j]; }

int p2(int p) {
	return (p *= 2) > cnt[t] ? 0 : (p < cnt[t] && lt(iq[t][p + 1], iq[t][p]) ? p + 1 : p);
}

void pq_up(int i) {
	int p, q, j;

	for (p = pq[i]; (q = p / 2) && lt(i, j = iq[t][q]); p = q)
		iq[t][pq[j] = p] = j;
	iq[t][pq[i] = p] = i;
}

void pq_dn(int i) {
	int p, q, j;

	for (p = pq[i]; (q = p2(p)) && lt(j = iq[t][q], i); p = q)
		iq[t][pq[j] = p] = j;
	iq[t][pq[i] = p] = i;
}

void pq_add(int i) {
	pq[i] = ++cnt[t], tt[i] = t, pq_up(i);
}

int pq_remove(int i) {
	int j = iq[t][cnt[t]--];

	if (j != i)
		pq[j] = pq[i], pq_up(j), pq_dn(j);
	pq[i] = 0, tt[i] = -1;
	return i;
}

long long sum; int x;

long long cost(int t, int i) {
	return (long long) cc[t][i] * ((t & 1) == 0 ? -1 : 1) + (1 - (t / 2)) * x;
}

void balance() {
	int t1, t2, i1, i1_, i2, i2_;
	long long c1, c1_, c2, c2_;

check:
	for (t1 = 0; t1 < 6; t1 += 2)
		for (t2 = 1; t2 < 6; t2 += 2)
			if (cnt[t1] && cnt[t2]) {
				i1 = iq[t1][1], i2 = iq[t2][1];
				if (cost(t1, i1) > cost(t2, i2)) {
					t = t1, pq_remove(i1), sum -= -cc[t1][i1];
					t = t1 ^ 1, pq_add(i1);
					t = t2, pq_remove(i2);
					t = t2 ^ 1, pq_add(i2), sum += -cc[t2 ^ 1][i2];
					goto check;
				}
			}
	while (cnt[0] + cnt[2] + cnt[4] < k) {
		c2_ = INF, i2_ = -1;
		for (t2 = 1; t2 < 6; t2 += 2)
			if (cnt[t2]) {
				i2 = iq[t2][1], c2 = cost(t2, i2);
				if (c2_ > c2)
					c2_ = c2, i2_ = i2;
			}
		i2 = i2_, t2 = tt[i2];
		t = t2, pq_remove(i2);
		t = t2 ^ 1, pq_add(i2), sum += -cc[t2 ^ 1][i2];
	}
	while (cnt[0] + cnt[2] + cnt[4] > k) {
		c1_ = -INF, i1_ = -1;
		for (t1 = 0; t1 < 6; t1 += 2)
			if (cnt[t1]) {
				i1 = iq[t1][1], c1 = cost(t1, i1);
				if (c1_ < c1)
					c1_ = c1, i1_ = i1;
			}
		i1 = i1_, t1 = tt[i1];
		t = t1, pq_remove(i1), sum -= -cc[t1][i1];
		t = t1 ^ 1, pq_add(i1);
	}
}

long long eval() {
	return sum + (long long) (cnt[0] - cnt[4]) * x;
}

int main() {
	static int ww[N], ii[N * 2];
	int h, i, i_;
	long long w, 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[0][i] = -(ww[i] - xx[i << 1 | 1]), cc[1][i] = ww[i] - xx[i << 1 | 1];
		cc[2][i] = -ww[i], cc[3][i] = ww[i];
		cc[4][i] = -(ww[i] + xx[i << 1 | 0]), cc[5][i] = ww[i] + xx[i << 1 | 0];
	}
	for (i = 0; i < n * 2; i++)
		ii[i] = i;
	sort(ii, 0, n * 2);
	for (i = 0; i < n; i++)
		t = 5, pq_add(i);
	ans = INF;
	for (h = 0; h < n * 2; h++) {
		i_ = ii[h], i = i_ >> 1, x = xx[i_];
		if ((i_ & 1) == 0) {
			t = tt[i], pq_remove(i);
			if ((t & 1) == 0)
				sum -= -cc[t][i];
			t = 3, pq_add(i);
		} else {
			t = tt[i], pq_remove(i);
			if ((t & 1) == 0)
				sum -= -cc[t][i];
			t = 1, pq_add(i);
		}
		balance();
		ans = min(ans, eval() - w);
	}
	printf("%lld\n", ans);
	return 0;
}

Compilation message

radio.c: In function 'main':
radio.c:130:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  130 |  scanf("%d%d", &n, &k);
      |  ^~~~~~~~~~~~~~~~~~~~~
radio.c:135:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  135 |   scanf("%d%d%d", &x, &p, &ww[i]);
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 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 2 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 2 ms 340 KB Output is correct
4 Correct 2 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 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 2 ms 340 KB Output is correct
6 Correct 9 ms 600 KB Output is correct
7 Correct 31 ms 1612 KB Output is correct
8 Correct 101 ms 3532 KB Output is correct
9 Correct 170 ms 5544 KB Output is correct
10 Correct 185 ms 6540 KB Output is correct
11 Correct 235 ms 6812 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 2 ms 340 KB Output is correct
4 Correct 2 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 8 ms 596 KB Output is correct
8 Correct 35 ms 1680 KB Output is correct
9 Correct 113 ms 3488 KB Output is correct
10 Correct 169 ms 5276 KB Output is correct
11 Correct 88 ms 4168 KB Output is correct
12 Correct 205 ms 6504 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 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 0 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 2 ms 340 KB Output is correct
16 Correct 2 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 9 ms 600 KB Output is correct
20 Correct 31 ms 1612 KB Output is correct
21 Correct 101 ms 3532 KB Output is correct
22 Correct 170 ms 5544 KB Output is correct
23 Correct 185 ms 6540 KB Output is correct
24 Correct 235 ms 6812 KB Output is correct
25 Correct 8 ms 596 KB Output is correct
26 Correct 35 ms 1680 KB Output is correct
27 Correct 113 ms 3488 KB Output is correct
28 Correct 169 ms 5276 KB Output is correct
29 Correct 88 ms 4168 KB Output is correct
30 Correct 205 ms 6504 KB Output is correct
31 Correct 1 ms 340 KB Output is correct
32 Correct 70 ms 2276 KB Output is correct
33 Correct 96 ms 3144 KB Output is correct
34 Correct 119 ms 3592 KB Output is correct
35 Correct 120 ms 4276 KB Output is correct
36 Correct 255 ms 6724 KB Output is correct
37 Correct 274 ms 8700 KB Output is correct
38 Correct 229 ms 8012 KB Output is correct
39 Correct 119 ms 7996 KB Output is correct
40 Correct 263 ms 8960 KB Output is correct