Submission #485085

# Submission time Handle Problem Language Result Execution time Memory
485085 2021-11-06T06:35:34 Z arujbansal Group Photo (JOI21_ho_t3) C++17
100 / 100
474 ms 728 KB
#include <bits/stdc++.h>

using namespace std;

#define int long long

const int MXN = 2e5 + 5, INF = 1e9 + 5;
int N;
int A[MXN], dp[MXN], pos[MXN];

struct fenwick_tree {
	int n;
	vector<int> tree;

	fenwick_tree(int _n) {
		n = _n;
		tree.assign(n + 1, 0);
	}

	void increment(int pos, int val) {
		for (int i = pos; i <= n; i += i & -i)
			tree[i] += val;
	}

	int query(int pos) {
		int res = 0;

		for (int i = pos; i > 0; i -= i & -i)
			res += tree[i];

		return res;
	}
};

signed main() {
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);

	cin >> N;

	for (int i = 1; i <= N; i++) {
		cin >> A[i];
		dp[i] = INF;
	}

	for (int i = 1; i <= N; i++) {
		// Consider an array of all A[i] <= i, maintain relative positions only - compress
		for (int j = 1, cur_pos = 1; j <= N; j++) {
			if (A[j] <= i)
				pos[A[j]] = cur_pos++;
		}

		fenwick_tree ft(N + 1);

		int cost = 0;
		for (int j = i; j >= 1; j--) {
			int loc = pos[j] - ft.query(pos[j]);
			cost += i - loc - 2 * ft.query(pos[j] - 1);
			ft.increment(pos[j], 1);


			dp[i] = min(dp[i], dp[j - 1] + cost);
		}
	}

	cout << dp[N];
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 332 KB Output is correct
2 Correct 0 ms 332 KB Output is correct
3 Correct 0 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 0 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 0 ms 332 KB Output is correct
9 Correct 0 ms 332 KB Output is correct
10 Correct 0 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 332 KB Output is correct
2 Correct 0 ms 332 KB Output is correct
3 Correct 0 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 0 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 0 ms 332 KB Output is correct
9 Correct 0 ms 332 KB Output is correct
10 Correct 0 ms 332 KB Output is correct
11 Correct 0 ms 332 KB Output is correct
12 Correct 0 ms 268 KB Output is correct
13 Correct 0 ms 332 KB Output is correct
14 Correct 0 ms 332 KB Output is correct
15 Correct 0 ms 332 KB Output is correct
16 Correct 0 ms 332 KB Output is correct
17 Correct 0 ms 332 KB Output is correct
18 Correct 0 ms 332 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 1 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 332 KB Output is correct
2 Correct 0 ms 332 KB Output is correct
3 Correct 0 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 0 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 0 ms 332 KB Output is correct
9 Correct 0 ms 332 KB Output is correct
10 Correct 0 ms 332 KB Output is correct
11 Correct 0 ms 332 KB Output is correct
12 Correct 0 ms 268 KB Output is correct
13 Correct 0 ms 332 KB Output is correct
14 Correct 0 ms 332 KB Output is correct
15 Correct 0 ms 332 KB Output is correct
16 Correct 0 ms 332 KB Output is correct
17 Correct 0 ms 332 KB Output is correct
18 Correct 0 ms 332 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 1 ms 332 KB Output is correct
21 Correct 1 ms 332 KB Output is correct
22 Correct 1 ms 332 KB Output is correct
23 Correct 1 ms 332 KB Output is correct
24 Correct 1 ms 332 KB Output is correct
25 Correct 1 ms 332 KB Output is correct
26 Correct 1 ms 332 KB Output is correct
27 Correct 1 ms 332 KB Output is correct
28 Correct 1 ms 332 KB Output is correct
29 Correct 1 ms 332 KB Output is correct
30 Correct 1 ms 320 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 332 KB Output is correct
2 Correct 0 ms 332 KB Output is correct
3 Correct 0 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 0 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 0 ms 332 KB Output is correct
9 Correct 0 ms 332 KB Output is correct
10 Correct 0 ms 332 KB Output is correct
11 Correct 0 ms 332 KB Output is correct
12 Correct 0 ms 268 KB Output is correct
13 Correct 0 ms 332 KB Output is correct
14 Correct 0 ms 332 KB Output is correct
15 Correct 0 ms 332 KB Output is correct
16 Correct 0 ms 332 KB Output is correct
17 Correct 0 ms 332 KB Output is correct
18 Correct 0 ms 332 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 1 ms 332 KB Output is correct
21 Correct 1 ms 332 KB Output is correct
22 Correct 1 ms 332 KB Output is correct
23 Correct 1 ms 332 KB Output is correct
24 Correct 1 ms 332 KB Output is correct
25 Correct 1 ms 332 KB Output is correct
26 Correct 1 ms 332 KB Output is correct
27 Correct 1 ms 332 KB Output is correct
28 Correct 1 ms 332 KB Output is correct
29 Correct 1 ms 332 KB Output is correct
30 Correct 1 ms 320 KB Output is correct
31 Correct 10 ms 332 KB Output is correct
32 Correct 10 ms 324 KB Output is correct
33 Correct 10 ms 364 KB Output is correct
34 Correct 10 ms 332 KB Output is correct
35 Correct 10 ms 368 KB Output is correct
36 Correct 10 ms 332 KB Output is correct
37 Correct 10 ms 364 KB Output is correct
38 Correct 11 ms 332 KB Output is correct
39 Correct 11 ms 332 KB Output is correct
40 Correct 10 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 332 KB Output is correct
2 Correct 0 ms 332 KB Output is correct
3 Correct 0 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 0 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 0 ms 332 KB Output is correct
9 Correct 0 ms 332 KB Output is correct
10 Correct 0 ms 332 KB Output is correct
11 Correct 0 ms 332 KB Output is correct
12 Correct 0 ms 268 KB Output is correct
13 Correct 0 ms 332 KB Output is correct
14 Correct 0 ms 332 KB Output is correct
15 Correct 0 ms 332 KB Output is correct
16 Correct 0 ms 332 KB Output is correct
17 Correct 0 ms 332 KB Output is correct
18 Correct 0 ms 332 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 1 ms 332 KB Output is correct
21 Correct 1 ms 332 KB Output is correct
22 Correct 1 ms 332 KB Output is correct
23 Correct 1 ms 332 KB Output is correct
24 Correct 1 ms 332 KB Output is correct
25 Correct 1 ms 332 KB Output is correct
26 Correct 1 ms 332 KB Output is correct
27 Correct 1 ms 332 KB Output is correct
28 Correct 1 ms 332 KB Output is correct
29 Correct 1 ms 332 KB Output is correct
30 Correct 1 ms 320 KB Output is correct
31 Correct 10 ms 332 KB Output is correct
32 Correct 10 ms 324 KB Output is correct
33 Correct 10 ms 364 KB Output is correct
34 Correct 10 ms 332 KB Output is correct
35 Correct 10 ms 368 KB Output is correct
36 Correct 10 ms 332 KB Output is correct
37 Correct 10 ms 364 KB Output is correct
38 Correct 11 ms 332 KB Output is correct
39 Correct 11 ms 332 KB Output is correct
40 Correct 10 ms 332 KB Output is correct
41 Correct 474 ms 484 KB Output is correct
42 Correct 431 ms 584 KB Output is correct
43 Correct 428 ms 596 KB Output is correct
44 Correct 434 ms 580 KB Output is correct
45 Correct 432 ms 728 KB Output is correct
46 Correct 436 ms 492 KB Output is correct
47 Correct 443 ms 480 KB Output is correct
48 Correct 469 ms 600 KB Output is correct
49 Correct 429 ms 600 KB Output is correct
50 Correct 431 ms 460 KB Output is correct
51 Correct 434 ms 476 KB Output is correct
52 Correct 438 ms 580 KB Output is correct
53 Correct 438 ms 472 KB Output is correct
54 Correct 437 ms 472 KB Output is correct
55 Correct 428 ms 624 KB Output is correct