답안 #471798

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
471798 2021-09-10T21:28:56 Z rainboy Krov (COCI17_krov) C
140 / 140
785 ms 6588 KB
/* https://codeforces.com/blog/entry/56372?#comment-400794 (koosaga) */
#include <stdio.h>

#define N	100000
#define INF	0x3f3f3f3f
#define LINF	0x3f3f3f3f3f3f3f3fLL

long long min(long long a, long long b) { return a < b ? a : b; }
int max(int a, int b) { return a > b ? a : b; }

unsigned int X = 12345;

int rand_() {
	return (X *= 3) >> 1;
}

int *zz;

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 (zz[ii[j]] == zz[i_])
				j++;
			else if (zz[ii[j]] < zz[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;
	}
}

void update(long long *ft, int i, int n, int x) {
	while (i < n) {
		ft[i] += x;
		i |= i + 1;
	}
}

long long query(long long *ft, int i) {
	long long x = 0;

	while (i >= 0) {
		x += ft[i];
		i &= i + 1, i--;
	}
	return x;
}

int aa_[N], bb_[N]; long long ft1[N], ft2[N], ft3[N], ft4[N];

int search(int *aa, int n, int a) {
	int lower = -1, upper = n;

	while (upper - lower > 1) {
		int i_ = (lower + upper) / 2;

		if (aa[i_] <= a)
			lower = i_;
		else
			upper = i_;
	}
	return lower;
}

int main() {
	static int xx[N], aa[N], bb[N], ii[N];
	int n, i;
	long long ans;

	scanf("%d", &n);
	for (i = 0; i < n; i++) {
		scanf("%d", &xx[i]);
		aa[i] = xx[i] - i, bb[i] = xx[i] + i;
	}
	for (i = 0; i < n; i++)
		ii[i] = i;
	zz = aa, sort(ii, 0, n);
	for (i = 0; i < n; i++)
		aa_[i] = aa[ii[i]], aa[ii[i]] = i;
	zz = bb, sort(ii, 0, n);
	for (i = 0; i < n; i++)
		bb_[i] = bb[ii[i]], bb[ii[i]] = i;
	ans = LINF;
	for (i = 0; i < n; i++)
		update(ft3, bb[i], n, 1), update(ft4, bb[i], n, bb_[bb[i]]);
	for (i = 0; i < n; i++) {
		int lower, upper, x, il, ir;

		update(ft3, bb[i], n, -1), update(ft4, bb[i], n, -bb_[bb[i]]);
		update(ft1, aa[i], n, 1), update(ft2, aa[i], n, aa_[aa[i]]);
		lower = max(i + 1, n - i) - 1, upper = INF;
		while (upper - lower > 1) {
			x = (lower + upper) / 2;
			if ((query(ft1, search(aa_, n, x - i)) + query(ft3, search(bb_, n, x + i))) * 2 >= n)
				upper = x;
			else
				lower = x;
		}
		x = upper;
		il = search(aa_, n, x - i), ir = search(bb_, n, x + i);
		ans = min(ans, query(ft1, il) * (x - i) - query(ft2, il)
				+ (query(ft2, n - 1) - query(ft2, il)) - (query(ft1, n - 1) - query(ft1, il)) * (x - i)
				+ query(ft3, ir) * (x + i) - query(ft4, ir)
				+ (query(ft4, n - 1) - query(ft4, ir)) - (query(ft3, n - 1) - query(ft3, ir)) * (x + i));
	}
	printf("%lld\n", ans);
	return 0;
}

Compilation message

krov.c: In function 'main':
krov.c:76:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   76 |  scanf("%d", &n);
      |  ^~~~~~~~~~~~~~~
krov.c:78:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   78 |   scanf("%d", &xx[i]);
      |   ^~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 332 KB Output is correct
2 Correct 5 ms 392 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 332 KB Output is correct
2 Correct 6 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 404 KB Output is correct
2 Correct 9 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 436 KB Output is correct
2 Correct 11 ms 460 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 460 KB Output is correct
2 Correct 13 ms 472 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 27 ms 460 KB Output is correct
2 Correct 25 ms 552 KB Output is correct
3 Correct 14 ms 488 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 172 ms 1752 KB Output is correct
2 Correct 172 ms 1900 KB Output is correct
3 Correct 172 ms 1816 KB Output is correct
4 Correct 212 ms 2128 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 297 ms 2472 KB Output is correct
2 Correct 330 ms 2956 KB Output is correct
3 Correct 285 ms 2884 KB Output is correct
4 Correct 268 ms 2668 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 549 ms 4088 KB Output is correct
2 Correct 549 ms 5056 KB Output is correct
3 Correct 272 ms 2636 KB Output is correct
4 Correct 599 ms 4888 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 785 ms 5732 KB Output is correct
2 Correct 765 ms 6588 KB Output is correct
3 Correct 585 ms 4868 KB Output is correct
4 Correct 785 ms 6144 KB Output is correct
5 Correct 172 ms 1860 KB Output is correct