답안 #524368

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
524368 2022-02-09T06:36:08 Z CPSC Group Photo (JOI21_ho_t3) C++14
100 / 100
914 ms 134504 KB
# include <bits/stdc++.h>
#define f first
#define s second
#define pb push_back
using namespace std;
const int N = 5005;
int n,a[N],idx[N],dp1[N][N],dp2[N][N],dp[N];
int tree[N];
void update(int idx, int val) {
    for (int i = idx; i < N; i+=i&(-i)) {
        tree[i] += val;
    }
}
int getans(int le, int ri) {
    int pas = 0;
    for (int i = ri; i > 0; i-=i&(-i)) {
        pas += tree[i];
    }
    for (int i = le - 1; i > 0; i-=i&(-i)){
        pas -= tree[i];
    }
    return pas;
}
main() {
    cin>>n;
    for (int i = 1; i <= n; i++) {
        cin>>a[i];
        idx[a[i]] = i;
    }
    for (int i = 1; i <= n; i++) {
        update(idx[i],1);
        for (int j = i + 1; j <= n; j++) {
            dp1[i][j] = dp1[i][j - 1] + getans(idx[j],n);
        }
    }
    for (int i = 1; i < N; i++) tree[i] = 0;
    for (int i = 1; i <= n; i++) {
        for (int j = i; j <= n; j++) {
            dp2[i][j] = dp2[i][j - 1] + getans(1, idx[j] - 1);
            update(idx[j],1);
        }
        for (int j = i; j <= n; j++) {
            update(idx[j], -1);
        }
    }
    dp[0] = 0;
    for (int i = 1; i <= n; i++) {
        dp[i] = 1e9;
        for (int j = i - 1; j >= 0; j--) {
            // j ---> 1   +  i ---> j + 1
            dp[i] = min(dp[i],dp[j] + dp1[j][i] + dp2[j + 1][i]);
        }
    }
    cout<<dp[n]<<endl;
}

Compilation message

Main.cpp:24:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   24 | main() {
      | ^~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 308 KB Output is correct
2 Correct 1 ms 304 KB Output is correct
3 Correct 1 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 1 ms 332 KB Output is correct
7 Correct 1 ms 300 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 428 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 308 KB Output is correct
2 Correct 1 ms 304 KB Output is correct
3 Correct 1 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 1 ms 332 KB Output is correct
7 Correct 1 ms 300 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 428 KB Output is correct
11 Correct 1 ms 460 KB Output is correct
12 Correct 1 ms 460 KB Output is correct
13 Correct 1 ms 460 KB Output is correct
14 Correct 1 ms 460 KB Output is correct
15 Correct 1 ms 432 KB Output is correct
16 Correct 1 ms 460 KB Output is correct
17 Correct 1 ms 460 KB Output is correct
18 Correct 1 ms 428 KB Output is correct
19 Correct 1 ms 432 KB Output is correct
20 Correct 1 ms 428 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 308 KB Output is correct
2 Correct 1 ms 304 KB Output is correct
3 Correct 1 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 1 ms 332 KB Output is correct
7 Correct 1 ms 300 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 428 KB Output is correct
11 Correct 1 ms 460 KB Output is correct
12 Correct 1 ms 460 KB Output is correct
13 Correct 1 ms 460 KB Output is correct
14 Correct 1 ms 460 KB Output is correct
15 Correct 1 ms 432 KB Output is correct
16 Correct 1 ms 460 KB Output is correct
17 Correct 1 ms 460 KB Output is correct
18 Correct 1 ms 428 KB Output is correct
19 Correct 1 ms 432 KB Output is correct
20 Correct 1 ms 428 KB Output is correct
21 Correct 2 ms 1996 KB Output is correct
22 Correct 2 ms 1996 KB Output is correct
23 Correct 2 ms 1996 KB Output is correct
24 Correct 2 ms 1996 KB Output is correct
25 Correct 2 ms 1996 KB Output is correct
26 Correct 3 ms 2096 KB Output is correct
27 Correct 3 ms 1996 KB Output is correct
28 Correct 2 ms 1996 KB Output is correct
29 Correct 3 ms 1996 KB Output is correct
30 Correct 3 ms 1996 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 308 KB Output is correct
2 Correct 1 ms 304 KB Output is correct
3 Correct 1 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 1 ms 332 KB Output is correct
7 Correct 1 ms 300 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 428 KB Output is correct
11 Correct 1 ms 460 KB Output is correct
12 Correct 1 ms 460 KB Output is correct
13 Correct 1 ms 460 KB Output is correct
14 Correct 1 ms 460 KB Output is correct
15 Correct 1 ms 432 KB Output is correct
16 Correct 1 ms 460 KB Output is correct
17 Correct 1 ms 460 KB Output is correct
18 Correct 1 ms 428 KB Output is correct
19 Correct 1 ms 432 KB Output is correct
20 Correct 1 ms 428 KB Output is correct
21 Correct 2 ms 1996 KB Output is correct
22 Correct 2 ms 1996 KB Output is correct
23 Correct 2 ms 1996 KB Output is correct
24 Correct 2 ms 1996 KB Output is correct
25 Correct 2 ms 1996 KB Output is correct
26 Correct 3 ms 2096 KB Output is correct
27 Correct 3 ms 1996 KB Output is correct
28 Correct 2 ms 1996 KB Output is correct
29 Correct 3 ms 1996 KB Output is correct
30 Correct 3 ms 1996 KB Output is correct
31 Correct 23 ms 9128 KB Output is correct
32 Correct 24 ms 9176 KB Output is correct
33 Correct 23 ms 9264 KB Output is correct
34 Correct 22 ms 9216 KB Output is correct
35 Correct 22 ms 9224 KB Output is correct
36 Correct 23 ms 9180 KB Output is correct
37 Correct 23 ms 9160 KB Output is correct
38 Correct 22 ms 9156 KB Output is correct
39 Correct 22 ms 9292 KB Output is correct
40 Correct 22 ms 9208 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 308 KB Output is correct
2 Correct 1 ms 304 KB Output is correct
3 Correct 1 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 1 ms 332 KB Output is correct
7 Correct 1 ms 300 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 428 KB Output is correct
11 Correct 1 ms 460 KB Output is correct
12 Correct 1 ms 460 KB Output is correct
13 Correct 1 ms 460 KB Output is correct
14 Correct 1 ms 460 KB Output is correct
15 Correct 1 ms 432 KB Output is correct
16 Correct 1 ms 460 KB Output is correct
17 Correct 1 ms 460 KB Output is correct
18 Correct 1 ms 428 KB Output is correct
19 Correct 1 ms 432 KB Output is correct
20 Correct 1 ms 428 KB Output is correct
21 Correct 2 ms 1996 KB Output is correct
22 Correct 2 ms 1996 KB Output is correct
23 Correct 2 ms 1996 KB Output is correct
24 Correct 2 ms 1996 KB Output is correct
25 Correct 2 ms 1996 KB Output is correct
26 Correct 3 ms 2096 KB Output is correct
27 Correct 3 ms 1996 KB Output is correct
28 Correct 2 ms 1996 KB Output is correct
29 Correct 3 ms 1996 KB Output is correct
30 Correct 3 ms 1996 KB Output is correct
31 Correct 23 ms 9128 KB Output is correct
32 Correct 24 ms 9176 KB Output is correct
33 Correct 23 ms 9264 KB Output is correct
34 Correct 22 ms 9216 KB Output is correct
35 Correct 22 ms 9224 KB Output is correct
36 Correct 23 ms 9180 KB Output is correct
37 Correct 23 ms 9160 KB Output is correct
38 Correct 22 ms 9156 KB Output is correct
39 Correct 22 ms 9292 KB Output is correct
40 Correct 22 ms 9208 KB Output is correct
41 Correct 876 ms 134504 KB Output is correct
42 Correct 887 ms 134336 KB Output is correct
43 Correct 914 ms 134396 KB Output is correct
44 Correct 897 ms 134428 KB Output is correct
45 Correct 891 ms 134452 KB Output is correct
46 Correct 887 ms 134412 KB Output is correct
47 Correct 883 ms 134444 KB Output is correct
48 Correct 882 ms 134480 KB Output is correct
49 Correct 881 ms 134496 KB Output is correct
50 Correct 880 ms 134280 KB Output is correct
51 Correct 887 ms 134444 KB Output is correct
52 Correct 886 ms 134476 KB Output is correct
53 Correct 904 ms 134400 KB Output is correct
54 Correct 891 ms 134428 KB Output is correct
55 Correct 882 ms 134320 KB Output is correct