답안 #625119

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
625119 2022-08-09T12:07:42 Z boris_mihov Group Photo (JOI21_ho_t3) C++14
100 / 100
418 ms 588 KB
#include <algorithm>
#include <iostream>
#include <numeric>
#include <vector>

typedef long long llong;
const int MAXN = 5000 + 10;
const int INF  = 1e9;

int dp[MAXN];
int treePos[MAXN];
int treeSmaller[MAXN];
int a[MAXN], n;
int pos[MAXN];

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

int query(int tree[], int pos)
{
    int sum = 0;
    for (int i = pos ; i >= 1 ; i -= i & (-i))
    {
        sum += tree[i];
    }

    return sum;
}

void solve()
{
    dp[n+1] = 0;
    for (int from = n ; from >= 1 ; --from)
    {
        std::fill(treeSmaller + 1, treeSmaller + 1 + n, 0);
        update(treePos, pos[from], 1);
        dp[from] = INF;
        int add = 0;

        for (int next = from ; next <= n ; ++next)
        {
            update(treeSmaller, pos[next], 1);
            add += query(treePos, pos[next]) - 1;
            add -= query(treeSmaller, n) - query(treeSmaller, pos[next]);
            dp[from] = std::min(dp[from], dp[next + 1] + add);
        }
    }

    std::cout << dp[1] << '\n';
}

void read()
{
    std::cin >> n;
    for (int i = 1 ; i <= n ; ++i)
    {
        std::cin >> a[i];
        pos[a[i]] = i;
    }
}

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

int main()
{
    fastIO();
    read();
    solve();

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 0 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 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 0 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 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 0 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 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 0 ms 212 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
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 0 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 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 0 ms 212 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 8 ms 348 KB Output is correct
32 Correct 9 ms 340 KB Output is correct
33 Correct 9 ms 352 KB Output is correct
34 Correct 8 ms 340 KB Output is correct
35 Correct 8 ms 340 KB Output is correct
36 Correct 8 ms 340 KB Output is correct
37 Correct 8 ms 352 KB Output is correct
38 Correct 7 ms 340 KB Output is correct
39 Correct 8 ms 348 KB Output is correct
40 Correct 9 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 0 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 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 0 ms 212 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 8 ms 348 KB Output is correct
32 Correct 9 ms 340 KB Output is correct
33 Correct 9 ms 352 KB Output is correct
34 Correct 8 ms 340 KB Output is correct
35 Correct 8 ms 340 KB Output is correct
36 Correct 8 ms 340 KB Output is correct
37 Correct 8 ms 352 KB Output is correct
38 Correct 7 ms 340 KB Output is correct
39 Correct 8 ms 348 KB Output is correct
40 Correct 9 ms 340 KB Output is correct
41 Correct 406 ms 340 KB Output is correct
42 Correct 413 ms 424 KB Output is correct
43 Correct 417 ms 340 KB Output is correct
44 Correct 418 ms 588 KB Output is correct
45 Correct 408 ms 432 KB Output is correct
46 Correct 381 ms 428 KB Output is correct
47 Correct 386 ms 408 KB Output is correct
48 Correct 389 ms 340 KB Output is correct
49 Correct 388 ms 548 KB Output is correct
50 Correct 396 ms 340 KB Output is correct
51 Correct 385 ms 424 KB Output is correct
52 Correct 398 ms 420 KB Output is correct
53 Correct 387 ms 424 KB Output is correct
54 Correct 391 ms 420 KB Output is correct
55 Correct 387 ms 420 KB Output is correct