Submission #784775

# Submission time Handle Problem Language Result Execution time Memory
784775 2023-07-16T13:56:14 Z borisAngelov Group Photo (JOI21_ho_t3) C++17
100 / 100
348 ms 588 KB
#include <bits/stdc++.h>

using namespace std;

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

int n;

int p[maxn];
int pos[maxn];

int dp[maxn];

int tree_original[maxn];

void update_original(int idx, int val)
{
    for (int i = idx; i <= n; i += (i & (-i)))
    {
        tree_original[i] += val;
    }
}

int query_original(int idx)
{
    int result = 0;

    for (int i = idx; i >= 1; i -= (i & (-i)))
    {
        result += tree_original[i];
    }

    return result;
}

int tree[maxn];

void reset_tree()
{
    for (int i = 1; i <= n; ++i)
    {
        tree[i] = 0;
    }
}

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

int query_tree(int idx)
{
    int result = 0;

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

    return result;
}

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 >> p[i];
        pos[p[i]] = i;
    }

    dp[n + 1] = 0;

    for (int i = n; i >= 1; --i)
    {
        update_original(pos[i], +1);
        reset_tree();

        dp[i] = 1e9;

        int cost = 0;

        for (int j = i; j <= n; ++j)
        {
            cost += query_original(pos[j]) - 1;

            update_tree(pos[j], +1);

            cost -= (query_tree(n) - query_tree(pos[j]));

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

    cout << dp[1] << endl;

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 1 ms 256 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 0 ms 340 KB Output is correct
18 Correct 0 ms 340 KB Output is correct
19 Correct 0 ms 340 KB Output is correct
20 Correct 0 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 1 ms 256 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 0 ms 340 KB Output is correct
18 Correct 0 ms 340 KB Output is correct
19 Correct 0 ms 340 KB Output is correct
20 Correct 0 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 1 ms 256 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 0 ms 340 KB Output is correct
18 Correct 0 ms 340 KB Output is correct
19 Correct 0 ms 340 KB Output is correct
20 Correct 0 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
31 Correct 5 ms 340 KB Output is correct
32 Correct 5 ms 340 KB Output is correct
33 Correct 5 ms 340 KB Output is correct
34 Correct 4 ms 340 KB Output is correct
35 Correct 4 ms 340 KB Output is correct
36 Correct 4 ms 340 KB Output is correct
37 Correct 4 ms 340 KB Output is correct
38 Correct 4 ms 340 KB Output is correct
39 Correct 4 ms 340 KB Output is correct
40 Correct 4 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 1 ms 256 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 0 ms 340 KB Output is correct
18 Correct 0 ms 340 KB Output is correct
19 Correct 0 ms 340 KB Output is correct
20 Correct 0 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
31 Correct 5 ms 340 KB Output is correct
32 Correct 5 ms 340 KB Output is correct
33 Correct 5 ms 340 KB Output is correct
34 Correct 4 ms 340 KB Output is correct
35 Correct 4 ms 340 KB Output is correct
36 Correct 4 ms 340 KB Output is correct
37 Correct 4 ms 340 KB Output is correct
38 Correct 4 ms 340 KB Output is correct
39 Correct 4 ms 340 KB Output is correct
40 Correct 4 ms 340 KB Output is correct
41 Correct 340 ms 392 KB Output is correct
42 Correct 343 ms 588 KB Output is correct
43 Correct 338 ms 340 KB Output is correct
44 Correct 348 ms 432 KB Output is correct
45 Correct 336 ms 436 KB Output is correct
46 Correct 332 ms 428 KB Output is correct
47 Correct 326 ms 416 KB Output is correct
48 Correct 325 ms 340 KB Output is correct
49 Correct 329 ms 340 KB Output is correct
50 Correct 325 ms 412 KB Output is correct
51 Correct 332 ms 420 KB Output is correct
52 Correct 329 ms 428 KB Output is correct
53 Correct 337 ms 424 KB Output is correct
54 Correct 331 ms 424 KB Output is correct
55 Correct 332 ms 432 KB Output is correct