Submission #792695

# Submission time Handle Problem Language Result Execution time Memory
792695 2023-07-25T08:08:30 Z GEN 이지후(#10086) Line Town (CCO23_day1problem3) C++17
25 / 25
328 ms 29796 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, bit3;

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]);
			vector<int> seq0(len), seq1(len);
			{
				int pl[2] = {};
				int pr[2] = {sz(values[0]), sz(values[1])};
				for (int x = 0; x < len; x++) {
					int pos = (LS + x) % 2;
					if (pl[pos] < sz(values[pos])) {
						seq0[x] = values[pos][pl[pos]][2];
					}
					pl[pos]++;
				}
				for (int x = len - 1; x >= 0; x--) {
					int pos = (RS + len - 1 - x) % 2;
					pr[pos]--;
					if (pr[pos] >= 0)
						seq1[x] = values[pos][pr[pos]][2];
				}
			}
			lint curinv = 0;
			for (int i = len - 1; i >= 0; i--) {
				curinv += bit2.query(seq1[i] - 1);
				bit2.add(seq1[i], 1);
			}
			for (int k = 0; k <= len; k++) {
				int pl[2] = {};
				int pr[2] = {sz(values[0]), sz(values[1])};
				pl[LS] += (k + 1) / 2;
				pl[LS ^ 1] += k / 2;
				pr[RS] -= (len - k + 1) / 2;
				pr[RS ^ 1] -= (len - k) / 2;
				if (pl[0] == pr[0] && pl[1] == pr[1]) {
					lint cost = curinv;
					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);
				}
				if (k < len) {
					curinv -= bit2.query(seq1[k] - 1);
					curinv -= bit3.query(MAXN - 3) - bit3.query(seq1[k]);
					bit2.add(seq1[k], -1);
					curinv += bit2.query(seq0[k] - 1);
					curinv += bit3.query(MAXN - 3) - bit3.query(seq0[k]);
					bit3.add(seq0[k], +1);
				}
			}
			for (auto &x : seq0)
				bit3.add(x, -1);
		}
		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";
}
# Verdict Execution time Memory 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 1 ms 468 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 2 ms 468 KB Output is correct
8 Correct 1 ms 468 KB Output is correct
9 Correct 1 ms 468 KB Output is correct
10 Correct 1 ms 468 KB Output is correct
11 Correct 1 ms 468 KB Output is correct
12 Correct 1 ms 468 KB Output is correct
13 Correct 1 ms 468 KB Output is correct
# Verdict Execution time Memory 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 1 ms 468 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 2 ms 468 KB Output is correct
8 Correct 1 ms 468 KB Output is correct
9 Correct 1 ms 468 KB Output is correct
10 Correct 1 ms 468 KB Output is correct
11 Correct 1 ms 468 KB Output is correct
12 Correct 1 ms 468 KB Output is correct
13 Correct 1 ms 468 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 207 ms 29680 KB Output is correct
16 Correct 206 ms 29608 KB Output is correct
17 Correct 202 ms 29584 KB Output is correct
18 Correct 195 ms 29688 KB Output is correct
19 Correct 206 ms 29588 KB Output is correct
20 Correct 189 ms 29612 KB Output is correct
21 Correct 186 ms 29624 KB Output is correct
22 Correct 188 ms 29632 KB Output is correct
23 Correct 197 ms 29692 KB Output is correct
24 Correct 193 ms 29624 KB Output is correct
# Verdict Execution time Memory 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 1 ms 468 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 2 ms 468 KB Output is correct
8 Correct 1 ms 468 KB Output is correct
9 Correct 1 ms 468 KB Output is correct
10 Correct 1 ms 468 KB Output is correct
11 Correct 1 ms 468 KB Output is correct
12 Correct 1 ms 468 KB Output is correct
13 Correct 1 ms 468 KB Output is correct
14 Correct 1 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 1 ms 468 KB Output is correct
18 Correct 1 ms 468 KB Output is correct
19 Correct 1 ms 468 KB Output is correct
20 Correct 1 ms 468 KB Output is correct
21 Correct 1 ms 468 KB Output is correct
22 Correct 1 ms 468 KB Output is correct
23 Correct 1 ms 468 KB Output is correct
24 Correct 1 ms 468 KB Output is correct
25 Correct 1 ms 468 KB Output is correct
26 Correct 1 ms 468 KB Output is correct
27 Correct 1 ms 468 KB Output is correct
28 Correct 1 ms 468 KB Output is correct
29 Correct 1 ms 468 KB Output is correct
30 Correct 0 ms 340 KB Output is correct
31 Correct 1 ms 468 KB Output is correct
32 Correct 1 ms 468 KB Output is correct
# Verdict Execution time Memory 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 1 ms 468 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 2 ms 468 KB Output is correct
8 Correct 1 ms 468 KB Output is correct
9 Correct 1 ms 468 KB Output is correct
10 Correct 1 ms 468 KB Output is correct
11 Correct 1 ms 468 KB Output is correct
12 Correct 1 ms 468 KB Output is correct
13 Correct 1 ms 468 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 207 ms 29680 KB Output is correct
16 Correct 206 ms 29608 KB Output is correct
17 Correct 202 ms 29584 KB Output is correct
18 Correct 195 ms 29688 KB Output is correct
19 Correct 206 ms 29588 KB Output is correct
20 Correct 189 ms 29612 KB Output is correct
21 Correct 186 ms 29624 KB Output is correct
22 Correct 188 ms 29632 KB Output is correct
23 Correct 197 ms 29692 KB Output is correct
24 Correct 193 ms 29624 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 0 ms 340 KB Output is correct
27 Correct 0 ms 340 KB Output is correct
28 Correct 1 ms 468 KB Output is correct
29 Correct 1 ms 468 KB Output is correct
30 Correct 1 ms 468 KB Output is correct
31 Correct 1 ms 468 KB Output is correct
32 Correct 1 ms 468 KB Output is correct
33 Correct 1 ms 468 KB Output is correct
34 Correct 1 ms 468 KB Output is correct
35 Correct 1 ms 468 KB Output is correct
36 Correct 1 ms 468 KB Output is correct
37 Correct 1 ms 468 KB Output is correct
38 Correct 1 ms 468 KB Output is correct
39 Correct 1 ms 468 KB Output is correct
40 Correct 1 ms 468 KB Output is correct
41 Correct 0 ms 340 KB Output is correct
42 Correct 1 ms 468 KB Output is correct
43 Correct 1 ms 468 KB Output is correct
44 Correct 144 ms 21960 KB Output is correct
45 Correct 150 ms 21872 KB Output is correct
46 Correct 159 ms 21836 KB Output is correct
47 Correct 146 ms 21856 KB Output is correct
48 Correct 169 ms 24384 KB Output is correct
49 Correct 192 ms 29796 KB Output is correct
50 Correct 127 ms 20096 KB Output is correct
51 Correct 129 ms 19984 KB Output is correct
52 Correct 127 ms 20028 KB Output is correct
53 Correct 132 ms 20096 KB Output is correct
54 Correct 166 ms 23272 KB Output is correct
55 Correct 162 ms 23312 KB Output is correct
56 Correct 158 ms 23252 KB Output is correct
57 Correct 45 ms 10372 KB Output is correct
58 Correct 43 ms 10248 KB Output is correct
59 Correct 70 ms 10212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 2 ms 468 KB Output is correct
4 Correct 2 ms 468 KB Output is correct
5 Correct 2 ms 468 KB Output is correct
6 Correct 2 ms 468 KB Output is correct
7 Correct 2 ms 364 KB Output is correct
8 Correct 2 ms 360 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 2 ms 468 KB Output is correct
12 Correct 1 ms 468 KB Output is correct
13 Correct 1 ms 468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 2 ms 468 KB Output is correct
4 Correct 2 ms 468 KB Output is correct
5 Correct 2 ms 468 KB Output is correct
6 Correct 2 ms 468 KB Output is correct
7 Correct 2 ms 364 KB Output is correct
8 Correct 2 ms 360 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 2 ms 468 KB Output is correct
12 Correct 1 ms 468 KB Output is correct
13 Correct 1 ms 468 KB Output is correct
14 Correct 253 ms 14156 KB Output is correct
15 Correct 327 ms 14096 KB Output is correct
16 Correct 313 ms 14156 KB Output is correct
17 Correct 299 ms 14156 KB Output is correct
18 Correct 304 ms 14096 KB Output is correct
19 Correct 319 ms 14060 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 0 ms 340 KB Output is correct
22 Correct 285 ms 14180 KB Output is correct
23 Correct 290 ms 14240 KB Output is correct
24 Correct 228 ms 14092 KB Output is correct
# Verdict Execution time Memory 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 1 ms 468 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 2 ms 468 KB Output is correct
8 Correct 1 ms 468 KB Output is correct
9 Correct 1 ms 468 KB Output is correct
10 Correct 1 ms 468 KB Output is correct
11 Correct 1 ms 468 KB Output is correct
12 Correct 1 ms 468 KB Output is correct
13 Correct 1 ms 468 KB Output is correct
14 Correct 1 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 1 ms 468 KB Output is correct
18 Correct 1 ms 468 KB Output is correct
19 Correct 1 ms 468 KB Output is correct
20 Correct 1 ms 468 KB Output is correct
21 Correct 1 ms 468 KB Output is correct
22 Correct 1 ms 468 KB Output is correct
23 Correct 1 ms 468 KB Output is correct
24 Correct 1 ms 468 KB Output is correct
25 Correct 1 ms 468 KB Output is correct
26 Correct 1 ms 468 KB Output is correct
27 Correct 1 ms 468 KB Output is correct
28 Correct 1 ms 468 KB Output is correct
29 Correct 1 ms 468 KB Output is correct
30 Correct 0 ms 340 KB Output is correct
31 Correct 1 ms 468 KB Output is correct
32 Correct 1 ms 468 KB Output is correct
33 Correct 1 ms 340 KB Output is correct
34 Correct 1 ms 340 KB Output is correct
35 Correct 2 ms 468 KB Output is correct
36 Correct 2 ms 468 KB Output is correct
37 Correct 2 ms 468 KB Output is correct
38 Correct 2 ms 468 KB Output is correct
39 Correct 2 ms 364 KB Output is correct
40 Correct 2 ms 360 KB Output is correct
41 Correct 1 ms 340 KB Output is correct
42 Correct 0 ms 340 KB Output is correct
43 Correct 2 ms 468 KB Output is correct
44 Correct 1 ms 468 KB Output is correct
45 Correct 1 ms 468 KB Output is correct
46 Correct 2 ms 468 KB Output is correct
47 Correct 2 ms 364 KB Output is correct
48 Correct 2 ms 468 KB Output is correct
49 Correct 1 ms 468 KB Output is correct
50 Correct 2 ms 468 KB Output is correct
51 Correct 1 ms 468 KB Output is correct
52 Correct 1 ms 468 KB Output is correct
53 Correct 1 ms 468 KB Output is correct
54 Correct 1 ms 468 KB Output is correct
55 Correct 1 ms 468 KB Output is correct
56 Correct 0 ms 340 KB Output is correct
57 Correct 0 ms 340 KB Output is correct
# Verdict Execution time Memory 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 1 ms 468 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 2 ms 468 KB Output is correct
8 Correct 1 ms 468 KB Output is correct
9 Correct 1 ms 468 KB Output is correct
10 Correct 1 ms 468 KB Output is correct
11 Correct 1 ms 468 KB Output is correct
12 Correct 1 ms 468 KB Output is correct
13 Correct 1 ms 468 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 207 ms 29680 KB Output is correct
16 Correct 206 ms 29608 KB Output is correct
17 Correct 202 ms 29584 KB Output is correct
18 Correct 195 ms 29688 KB Output is correct
19 Correct 206 ms 29588 KB Output is correct
20 Correct 189 ms 29612 KB Output is correct
21 Correct 186 ms 29624 KB Output is correct
22 Correct 188 ms 29632 KB Output is correct
23 Correct 197 ms 29692 KB Output is correct
24 Correct 193 ms 29624 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 0 ms 340 KB Output is correct
27 Correct 0 ms 340 KB Output is correct
28 Correct 1 ms 468 KB Output is correct
29 Correct 1 ms 468 KB Output is correct
30 Correct 1 ms 468 KB Output is correct
31 Correct 1 ms 468 KB Output is correct
32 Correct 1 ms 468 KB Output is correct
33 Correct 1 ms 468 KB Output is correct
34 Correct 1 ms 468 KB Output is correct
35 Correct 1 ms 468 KB Output is correct
36 Correct 1 ms 468 KB Output is correct
37 Correct 1 ms 468 KB Output is correct
38 Correct 1 ms 468 KB Output is correct
39 Correct 1 ms 468 KB Output is correct
40 Correct 1 ms 468 KB Output is correct
41 Correct 0 ms 340 KB Output is correct
42 Correct 1 ms 468 KB Output is correct
43 Correct 1 ms 468 KB Output is correct
44 Correct 144 ms 21960 KB Output is correct
45 Correct 150 ms 21872 KB Output is correct
46 Correct 159 ms 21836 KB Output is correct
47 Correct 146 ms 21856 KB Output is correct
48 Correct 169 ms 24384 KB Output is correct
49 Correct 192 ms 29796 KB Output is correct
50 Correct 127 ms 20096 KB Output is correct
51 Correct 129 ms 19984 KB Output is correct
52 Correct 127 ms 20028 KB Output is correct
53 Correct 132 ms 20096 KB Output is correct
54 Correct 166 ms 23272 KB Output is correct
55 Correct 162 ms 23312 KB Output is correct
56 Correct 158 ms 23252 KB Output is correct
57 Correct 45 ms 10372 KB Output is correct
58 Correct 43 ms 10248 KB Output is correct
59 Correct 70 ms 10212 KB Output is correct
60 Correct 1 ms 340 KB Output is correct
61 Correct 1 ms 340 KB Output is correct
62 Correct 2 ms 468 KB Output is correct
63 Correct 2 ms 468 KB Output is correct
64 Correct 2 ms 468 KB Output is correct
65 Correct 2 ms 468 KB Output is correct
66 Correct 2 ms 364 KB Output is correct
67 Correct 2 ms 360 KB Output is correct
68 Correct 1 ms 340 KB Output is correct
69 Correct 0 ms 340 KB Output is correct
70 Correct 2 ms 468 KB Output is correct
71 Correct 1 ms 468 KB Output is correct
72 Correct 1 ms 468 KB Output is correct
73 Correct 253 ms 14156 KB Output is correct
74 Correct 327 ms 14096 KB Output is correct
75 Correct 313 ms 14156 KB Output is correct
76 Correct 299 ms 14156 KB Output is correct
77 Correct 304 ms 14096 KB Output is correct
78 Correct 319 ms 14060 KB Output is correct
79 Correct 1 ms 340 KB Output is correct
80 Correct 0 ms 340 KB Output is correct
81 Correct 285 ms 14180 KB Output is correct
82 Correct 290 ms 14240 KB Output is correct
83 Correct 228 ms 14092 KB Output is correct
84 Correct 2 ms 468 KB Output is correct
85 Correct 2 ms 364 KB Output is correct
86 Correct 2 ms 468 KB Output is correct
87 Correct 1 ms 468 KB Output is correct
88 Correct 2 ms 468 KB Output is correct
89 Correct 1 ms 468 KB Output is correct
90 Correct 1 ms 468 KB Output is correct
91 Correct 1 ms 468 KB Output is correct
92 Correct 1 ms 468 KB Output is correct
93 Correct 1 ms 468 KB Output is correct
94 Correct 0 ms 340 KB Output is correct
95 Correct 0 ms 340 KB Output is correct
96 Correct 184 ms 22064 KB Output is correct
97 Correct 173 ms 21596 KB Output is correct
98 Correct 211 ms 21652 KB Output is correct
99 Correct 282 ms 14088 KB Output is correct
100 Correct 172 ms 23500 KB Output is correct
101 Correct 322 ms 14360 KB Output is correct
102 Correct 305 ms 14336 KB Output is correct
103 Correct 328 ms 14036 KB Output is correct
104 Correct 270 ms 14028 KB Output is correct
105 Correct 307 ms 14244 KB Output is correct
106 Correct 239 ms 14620 KB Output is correct