답안 #792671

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
792671 2023-07-25T07:59:19 Z GEN 이지후(#10086) Line Town (CCO23_day1problem3) C++17
15 / 25
2000 ms 29900 KB
#include <bits/stdc++.h>
using namespace std;
using lint = long long;
using pi = array<lint, 2>;
#define sz(v) ((int)(v).size())
#define all(v) (v).begin(), (v).end()
const int MAXN = 500005;

struct bit {
	int tree[MAXN];
	void add(int x, int v) {
		for (int i = x + 2; i < MAXN; i += i & -i)
			tree[i] += v;
	}
	int query(int x) {
		int ret = 0;
		for (int i = x + 2; i; i -= i & -i)
			ret += tree[i];
		return ret;
	}
} bit, bit2;

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	int n;
	cin >> n;
	vector<pi> a(n);
	for (int i = 0; i < n; i++) {
		cin >> a[i][0];
		if (i % 2 == 1)
			a[i][0] *= -1;
		a[i][1] = i;
	}
	sort(all(a), [&](const pi &a, const pi &b) { return pi{abs(a[0]), a[1]} < pi{abs(b[0]), b[1]}; });
	lint dp[2] = {0, lint(1e18)};
	for (int i = 0; i < n; i++)
		bit.add(i, 1);
	for (int i = n - 1; i >= 0;) {
		int j = i;
		while (j >= 0 && abs(a[i][0]) == abs(a[j][0])) {
			bit.add(a[j][1], -1);
			j--;
		}
		if (abs(a[i][0]) == 0)
			break;
		vector<array<lint, 3>> values[2];
		for (int k = j + 1; k <= i; k++) {
			int L = bit.query(a[k][1]);
			int R = j + 1 - L;
			if (a[k][0] > 0)
				values[0].push_back({L, R, (int)a[k][1]});
			else
				values[1].push_back({L, R, (int)a[k][1]});
		}
		for (int i = 0; i < 2; i++) {
			for (int j = 1; j < sz(values[i]); j++) {
				values[i][j][0] += values[i][j - 1][0];
			}
			for (int j = sz(values[i]) - 2; j >= 0; j--) {
				values[i][j][1] += values[i][j + 1][1];
			}
		}
		lint nxt[2] = {lint(1e18), lint(1e18)};
		for (int j = 0; j < 2; j++) {
			int LS = (j + 1) % 2;
			int RS = (j + i) % 2;
			int len = sz(values[0]) + sz(values[1]);
			for (int k = 0; k <= len; k++) {
				int pl[2] = {};
				int pr[2] = {sz(values[0]), sz(values[1])};
				vector<int> seq, seqR;
				for (int l = 0; l < k; l++) {
					int pos = (LS + l) % 2;
					if (pl[pos] < sz(values[pos]))
						seq.push_back(values[pos][pl[pos]][2]);
					pl[pos]++;
				}
				for (int l = 0; l < len - k; l++) {
					int pos = (RS + l) % 2;
					pr[pos]--;
					if (pr[pos] >= 0)
						seqR.push_back(values[pos][pr[pos]][2]);
				}
				reverse(all(seqR));
				for (auto &x : seqR)
					seq.push_back(x);
				if (pl[0] == pr[0] && pl[1] == pr[1]) {
					lint cost = 0;
					for (int i = 0; i < sz(seq); i++) {
						bit2.add(seq[i], +1);
						cost += i + 1 - bit2.query(seq[i]);
					}
					for (int i = 0; i < sz(seq); i++) {
						bit2.add(seq[i], -1);
					}
					for (int i = 0; i < 2; i++) {
						if (pl[i])
							cost += values[i][pl[i] - 1][0];
						if (pl[i] < sz(values[i]))
							cost += values[i][pl[i]][1];
					}
					nxt[j ^ (k % 2)] = min(nxt[j ^ (k % 2)], dp[j] + cost);
				}
			}
		}
		dp[0] = nxt[0];
		dp[1] = nxt[1];
		i = j;
		// cout << dp[0] << " " << dp[1] << endl;
	}
	lint ans = min(dp[0], dp[1]);
	if (ans > 1e12)
		ans = -1;
	cout << ans << "\n";
}
# 결과 실행 시간 메모리 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 118 ms 512 KB Output is correct
5 Correct 137 ms 504 KB Output is correct
6 Correct 211 ms 468 KB Output is correct
7 Correct 28 ms 468 KB Output is correct
8 Correct 213 ms 508 KB Output is correct
9 Correct 191 ms 508 KB Output is correct
10 Correct 174 ms 508 KB Output is correct
11 Correct 176 ms 500 KB Output is correct
12 Correct 175 ms 500 KB Output is correct
13 Correct 108 ms 508 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 118 ms 512 KB Output is correct
5 Correct 137 ms 504 KB Output is correct
6 Correct 211 ms 468 KB Output is correct
7 Correct 28 ms 468 KB Output is correct
8 Correct 213 ms 508 KB Output is correct
9 Correct 191 ms 508 KB Output is correct
10 Correct 174 ms 508 KB Output is correct
11 Correct 176 ms 500 KB Output is correct
12 Correct 175 ms 500 KB Output is correct
13 Correct 108 ms 508 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Execution timed out 2058 ms 29900 KB Time limit exceeded
16 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 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 118 ms 512 KB Output is correct
5 Correct 137 ms 504 KB Output is correct
6 Correct 211 ms 468 KB Output is correct
7 Correct 28 ms 468 KB Output is correct
8 Correct 213 ms 508 KB Output is correct
9 Correct 191 ms 508 KB Output is correct
10 Correct 174 ms 508 KB Output is correct
11 Correct 176 ms 500 KB Output is correct
12 Correct 175 ms 500 KB Output is correct
13 Correct 108 ms 508 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 0 ms 340 KB Output is correct
17 Correct 50 ms 480 KB Output is correct
18 Correct 50 ms 480 KB Output is correct
19 Correct 66 ms 500 KB Output is correct
20 Correct 9 ms 340 KB Output is correct
21 Correct 177 ms 496 KB Output is correct
22 Correct 190 ms 504 KB Output is correct
23 Correct 44 ms 468 KB Output is correct
24 Correct 63 ms 468 KB Output is correct
25 Correct 61 ms 468 KB Output is correct
26 Correct 7 ms 452 KB Output is correct
27 Correct 81 ms 504 KB Output is correct
28 Correct 80 ms 468 KB Output is correct
29 Correct 152 ms 496 KB Output is correct
30 Correct 0 ms 340 KB Output is correct
31 Correct 1 ms 340 KB Output is correct
32 Correct 1 ms 360 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 118 ms 512 KB Output is correct
5 Correct 137 ms 504 KB Output is correct
6 Correct 211 ms 468 KB Output is correct
7 Correct 28 ms 468 KB Output is correct
8 Correct 213 ms 508 KB Output is correct
9 Correct 191 ms 508 KB Output is correct
10 Correct 174 ms 508 KB Output is correct
11 Correct 176 ms 500 KB Output is correct
12 Correct 175 ms 500 KB Output is correct
13 Correct 108 ms 508 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Execution timed out 2058 ms 29900 KB Time limit exceeded
16 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 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 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 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 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 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 151 ms 11980 KB Output is correct
15 Correct 234 ms 12200 KB Output is correct
16 Correct 213 ms 12128 KB Output is correct
17 Correct 213 ms 12176 KB Output is correct
18 Correct 210 ms 12168 KB Output is correct
19 Correct 233 ms 12104 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 206 ms 12340 KB Output is correct
23 Correct 185 ms 12332 KB Output is correct
24 Correct 153 ms 12120 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 118 ms 512 KB Output is correct
5 Correct 137 ms 504 KB Output is correct
6 Correct 211 ms 468 KB Output is correct
7 Correct 28 ms 468 KB Output is correct
8 Correct 213 ms 508 KB Output is correct
9 Correct 191 ms 508 KB Output is correct
10 Correct 174 ms 508 KB Output is correct
11 Correct 176 ms 500 KB Output is correct
12 Correct 175 ms 500 KB Output is correct
13 Correct 108 ms 508 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 0 ms 340 KB Output is correct
17 Correct 50 ms 480 KB Output is correct
18 Correct 50 ms 480 KB Output is correct
19 Correct 66 ms 500 KB Output is correct
20 Correct 9 ms 340 KB Output is correct
21 Correct 177 ms 496 KB Output is correct
22 Correct 190 ms 504 KB Output is correct
23 Correct 44 ms 468 KB Output is correct
24 Correct 63 ms 468 KB Output is correct
25 Correct 61 ms 468 KB Output is correct
26 Correct 7 ms 452 KB Output is correct
27 Correct 81 ms 504 KB Output is correct
28 Correct 80 ms 468 KB Output is correct
29 Correct 152 ms 496 KB Output is correct
30 Correct 0 ms 340 KB Output is correct
31 Correct 1 ms 340 KB Output is correct
32 Correct 1 ms 360 KB Output is correct
33 Correct 0 ms 340 KB Output is correct
34 Correct 0 ms 340 KB Output is correct
35 Correct 1 ms 340 KB Output is correct
36 Correct 1 ms 340 KB Output is correct
37 Correct 1 ms 340 KB Output is correct
38 Correct 1 ms 340 KB Output is correct
39 Correct 1 ms 340 KB Output is correct
40 Correct 1 ms 340 KB Output is correct
41 Correct 1 ms 340 KB Output is correct
42 Correct 0 ms 340 KB Output is correct
43 Correct 1 ms 340 KB Output is correct
44 Correct 1 ms 340 KB Output is correct
45 Correct 1 ms 340 KB Output is correct
46 Correct 2 ms 340 KB Output is correct
47 Correct 1 ms 340 KB Output is correct
48 Correct 2 ms 340 KB Output is correct
49 Correct 3 ms 340 KB Output is correct
50 Correct 17 ms 340 KB Output is correct
51 Correct 27 ms 484 KB Output is correct
52 Correct 44 ms 472 KB Output is correct
53 Correct 34 ms 484 KB Output is correct
54 Correct 1 ms 340 KB Output is correct
55 Correct 86 ms 504 KB Output is correct
56 Correct 0 ms 340 KB Output is correct
57 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 118 ms 512 KB Output is correct
5 Correct 137 ms 504 KB Output is correct
6 Correct 211 ms 468 KB Output is correct
7 Correct 28 ms 468 KB Output is correct
8 Correct 213 ms 508 KB Output is correct
9 Correct 191 ms 508 KB Output is correct
10 Correct 174 ms 508 KB Output is correct
11 Correct 176 ms 500 KB Output is correct
12 Correct 175 ms 500 KB Output is correct
13 Correct 108 ms 508 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Execution timed out 2058 ms 29900 KB Time limit exceeded
16 Halted 0 ms 0 KB -