답안 #1114163

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1114163 2024-11-18T09:03:42 Z borisAngelov Group Photo (JOI21_ho_t3) C++17
100 / 100
323 ms 832 KB
#include <bits/stdc++.h>

using namespace std;

const int maxn = 5005;
const int inf = 1e9;

int n;
int a[maxn], positions[maxn], toRem[maxn];

int dp[maxn];

struct FenwickTree
{
    int tree[maxn];

    void reset(int MAX)
    {
        fill(tree, tree + MAX + 1, 0);
    }

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

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

        for (; pos >= 1; pos -= (pos & (-pos)))
        {
            result += tree[pos];
        }

        return result;
    }
};

FenwickTree treeActive;
FenwickTree treeSmaller;

void fastIO()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
}

int main()
{
    fastIO();

    cin >> n;

    for (int i = 1; i <= n; ++i)
    {
        cin >> a[i];
        positions[a[i]] = i;
    }

    dp[n + 1] = 0;

    for (int num = n; num >= 1; --num)
    {
        dp[num] = inf;

        treeActive.update(positions[num], +1);
        treeSmaller.reset(n);

        int cost = 0;

        for (int to = num; to <= n; ++to)
        {
            cost += treeActive.query(positions[to]) - 1;
            treeSmaller.update(positions[to], +1);
            cost -= treeSmaller.query(n) - treeSmaller.query(positions[to]);
            dp[num] = min(dp[num], cost + dp[to + 1]);
        }
    }

    cout << dp[1] << endl;

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 504 KB Output is correct
3 Correct 1 ms 504 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 1 ms 336 KB Output is correct
8 Correct 1 ms 592 KB Output is correct
9 Correct 1 ms 336 KB Output is correct
10 Correct 1 ms 504 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 504 KB Output is correct
3 Correct 1 ms 504 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 1 ms 336 KB Output is correct
8 Correct 1 ms 592 KB Output is correct
9 Correct 1 ms 336 KB Output is correct
10 Correct 1 ms 504 KB Output is correct
11 Correct 1 ms 636 KB Output is correct
12 Correct 1 ms 336 KB Output is correct
13 Correct 1 ms 336 KB Output is correct
14 Correct 2 ms 336 KB Output is correct
15 Correct 1 ms 336 KB Output is correct
16 Correct 1 ms 336 KB Output is correct
17 Correct 1 ms 640 KB Output is correct
18 Correct 1 ms 504 KB Output is correct
19 Correct 1 ms 336 KB Output is correct
20 Correct 1 ms 504 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 504 KB Output is correct
3 Correct 1 ms 504 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 1 ms 336 KB Output is correct
8 Correct 1 ms 592 KB Output is correct
9 Correct 1 ms 336 KB Output is correct
10 Correct 1 ms 504 KB Output is correct
11 Correct 1 ms 636 KB Output is correct
12 Correct 1 ms 336 KB Output is correct
13 Correct 1 ms 336 KB Output is correct
14 Correct 2 ms 336 KB Output is correct
15 Correct 1 ms 336 KB Output is correct
16 Correct 1 ms 336 KB Output is correct
17 Correct 1 ms 640 KB Output is correct
18 Correct 1 ms 504 KB Output is correct
19 Correct 1 ms 336 KB Output is correct
20 Correct 1 ms 504 KB Output is correct
21 Correct 2 ms 336 KB Output is correct
22 Correct 2 ms 336 KB Output is correct
23 Correct 2 ms 336 KB Output is correct
24 Correct 1 ms 336 KB Output is correct
25 Correct 2 ms 336 KB Output is correct
26 Correct 1 ms 588 KB Output is correct
27 Correct 2 ms 336 KB Output is correct
28 Correct 1 ms 336 KB Output is correct
29 Correct 2 ms 336 KB Output is correct
30 Correct 1 ms 336 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 504 KB Output is correct
3 Correct 1 ms 504 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 1 ms 336 KB Output is correct
8 Correct 1 ms 592 KB Output is correct
9 Correct 1 ms 336 KB Output is correct
10 Correct 1 ms 504 KB Output is correct
11 Correct 1 ms 636 KB Output is correct
12 Correct 1 ms 336 KB Output is correct
13 Correct 1 ms 336 KB Output is correct
14 Correct 2 ms 336 KB Output is correct
15 Correct 1 ms 336 KB Output is correct
16 Correct 1 ms 336 KB Output is correct
17 Correct 1 ms 640 KB Output is correct
18 Correct 1 ms 504 KB Output is correct
19 Correct 1 ms 336 KB Output is correct
20 Correct 1 ms 504 KB Output is correct
21 Correct 2 ms 336 KB Output is correct
22 Correct 2 ms 336 KB Output is correct
23 Correct 2 ms 336 KB Output is correct
24 Correct 1 ms 336 KB Output is correct
25 Correct 2 ms 336 KB Output is correct
26 Correct 1 ms 588 KB Output is correct
27 Correct 2 ms 336 KB Output is correct
28 Correct 1 ms 336 KB Output is correct
29 Correct 2 ms 336 KB Output is correct
30 Correct 1 ms 336 KB Output is correct
31 Correct 8 ms 336 KB Output is correct
32 Correct 6 ms 336 KB Output is correct
33 Correct 6 ms 336 KB Output is correct
34 Correct 5 ms 336 KB Output is correct
35 Correct 6 ms 336 KB Output is correct
36 Correct 7 ms 336 KB Output is correct
37 Correct 5 ms 336 KB Output is correct
38 Correct 6 ms 336 KB Output is correct
39 Correct 5 ms 588 KB Output is correct
40 Correct 5 ms 504 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 504 KB Output is correct
3 Correct 1 ms 504 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 1 ms 336 KB Output is correct
8 Correct 1 ms 592 KB Output is correct
9 Correct 1 ms 336 KB Output is correct
10 Correct 1 ms 504 KB Output is correct
11 Correct 1 ms 636 KB Output is correct
12 Correct 1 ms 336 KB Output is correct
13 Correct 1 ms 336 KB Output is correct
14 Correct 2 ms 336 KB Output is correct
15 Correct 1 ms 336 KB Output is correct
16 Correct 1 ms 336 KB Output is correct
17 Correct 1 ms 640 KB Output is correct
18 Correct 1 ms 504 KB Output is correct
19 Correct 1 ms 336 KB Output is correct
20 Correct 1 ms 504 KB Output is correct
21 Correct 2 ms 336 KB Output is correct
22 Correct 2 ms 336 KB Output is correct
23 Correct 2 ms 336 KB Output is correct
24 Correct 1 ms 336 KB Output is correct
25 Correct 2 ms 336 KB Output is correct
26 Correct 1 ms 588 KB Output is correct
27 Correct 2 ms 336 KB Output is correct
28 Correct 1 ms 336 KB Output is correct
29 Correct 2 ms 336 KB Output is correct
30 Correct 1 ms 336 KB Output is correct
31 Correct 8 ms 336 KB Output is correct
32 Correct 6 ms 336 KB Output is correct
33 Correct 6 ms 336 KB Output is correct
34 Correct 5 ms 336 KB Output is correct
35 Correct 6 ms 336 KB Output is correct
36 Correct 7 ms 336 KB Output is correct
37 Correct 5 ms 336 KB Output is correct
38 Correct 6 ms 336 KB Output is correct
39 Correct 5 ms 588 KB Output is correct
40 Correct 5 ms 504 KB Output is correct
41 Correct 285 ms 336 KB Output is correct
42 Correct 295 ms 592 KB Output is correct
43 Correct 285 ms 832 KB Output is correct
44 Correct 295 ms 576 KB Output is correct
45 Correct 287 ms 588 KB Output is correct
46 Correct 273 ms 596 KB Output is correct
47 Correct 284 ms 572 KB Output is correct
48 Correct 287 ms 588 KB Output is correct
49 Correct 323 ms 336 KB Output is correct
50 Correct 278 ms 336 KB Output is correct
51 Correct 285 ms 488 KB Output is correct
52 Correct 297 ms 572 KB Output is correct
53 Correct 281 ms 592 KB Output is correct
54 Correct 274 ms 576 KB Output is correct
55 Correct 276 ms 336 KB Output is correct