답안 #870026

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
870026 2023-11-06T16:30:17 Z Pannda Group Photo (JOI21_ho_t3) C++17
100 / 100
329 ms 98804 KB
#include <bits/stdc++.h>
using namespace std;

const int INF = (int)1e9 + 123456;

struct Fenwick {
    int n;
    vector<int> bit;

    Fenwick(int n) : n(n), bit(n + 1, 0) {}

    void add(int i, int delta) {
        for (i++; i <= n; i += i & -i) {
            bit[i] += delta;
        }
    }

    int sum(int i) {
        int res = 0;
        for (; i > 0; i -= i & -i) {
            res += bit[i];
        }
        return res;
    }
};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n;
    cin >> n;
    vector<int> a(n);
    vector<int> ia(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
        a[i]--;
        ia[a[i]] = i;
    }

    vector<vector<int>> cost(n, vector<int>(n));
    vector<int> offset(n, 0);
    for (int l = 0; l < n; l++) {
        Fenwick fen(n);
        int inv = 0;
        int sum_indices = 0;
        for (int r = l; r < n; r++) {
            inv += fen.sum(ia[r]);
            fen.add(ia[r], +1);
            sum_indices += ia[r] + offset[ia[r]];

            cost[l][r] = inv + sum_indices - (r - l) * (r - l + 1) / 2;
        }
        for (int i = ia[l] + 1; i < n; i++) {
            offset[i]--;
        }
    }

    vector<int> dp(n + 1, INF);
    dp[0] = 0;
    for (int i = 0; i < n; i++) {
        for (int j = i; j >= 0; j--) {
            dp[i + 1] = min(dp[i + 1], dp[j] + cost[j][i]);
        }
    }
    cout << dp[n] << '\n';
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 1 ms 600 KB Output is correct
22 Correct 1 ms 620 KB Output is correct
23 Correct 1 ms 604 KB Output is correct
24 Correct 1 ms 604 KB Output is correct
25 Correct 0 ms 600 KB Output is correct
26 Correct 0 ms 604 KB Output is correct
27 Correct 0 ms 604 KB Output is correct
28 Correct 1 ms 604 KB Output is correct
29 Correct 0 ms 604 KB Output is correct
30 Correct 1 ms 604 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 1 ms 600 KB Output is correct
22 Correct 1 ms 620 KB Output is correct
23 Correct 1 ms 604 KB Output is correct
24 Correct 1 ms 604 KB Output is correct
25 Correct 0 ms 600 KB Output is correct
26 Correct 0 ms 604 KB Output is correct
27 Correct 0 ms 604 KB Output is correct
28 Correct 1 ms 604 KB Output is correct
29 Correct 0 ms 604 KB Output is correct
30 Correct 1 ms 604 KB Output is correct
31 Correct 4 ms 2908 KB Output is correct
32 Correct 5 ms 3000 KB Output is correct
33 Correct 4 ms 2908 KB Output is correct
34 Correct 4 ms 2908 KB Output is correct
35 Correct 4 ms 2908 KB Output is correct
36 Correct 4 ms 3004 KB Output is correct
37 Correct 6 ms 2908 KB Output is correct
38 Correct 4 ms 2908 KB Output is correct
39 Correct 4 ms 3008 KB Output is correct
40 Correct 4 ms 2908 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 1 ms 600 KB Output is correct
22 Correct 1 ms 620 KB Output is correct
23 Correct 1 ms 604 KB Output is correct
24 Correct 1 ms 604 KB Output is correct
25 Correct 0 ms 600 KB Output is correct
26 Correct 0 ms 604 KB Output is correct
27 Correct 0 ms 604 KB Output is correct
28 Correct 1 ms 604 KB Output is correct
29 Correct 0 ms 604 KB Output is correct
30 Correct 1 ms 604 KB Output is correct
31 Correct 4 ms 2908 KB Output is correct
32 Correct 5 ms 3000 KB Output is correct
33 Correct 4 ms 2908 KB Output is correct
34 Correct 4 ms 2908 KB Output is correct
35 Correct 4 ms 2908 KB Output is correct
36 Correct 4 ms 3004 KB Output is correct
37 Correct 6 ms 2908 KB Output is correct
38 Correct 4 ms 2908 KB Output is correct
39 Correct 4 ms 3008 KB Output is correct
40 Correct 4 ms 2908 KB Output is correct
41 Correct 308 ms 98384 KB Output is correct
42 Correct 312 ms 98436 KB Output is correct
43 Correct 315 ms 98384 KB Output is correct
44 Correct 320 ms 98536 KB Output is correct
45 Correct 311 ms 98644 KB Output is correct
46 Correct 320 ms 98548 KB Output is correct
47 Correct 317 ms 98564 KB Output is correct
48 Correct 311 ms 98552 KB Output is correct
49 Correct 310 ms 98388 KB Output is correct
50 Correct 319 ms 98804 KB Output is correct
51 Correct 311 ms 98548 KB Output is correct
52 Correct 329 ms 98552 KB Output is correct
53 Correct 311 ms 98384 KB Output is correct
54 Correct 321 ms 98552 KB Output is correct
55 Correct 313 ms 98392 KB Output is correct