답안 #587717

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
587717 2022-07-02T09:20:45 Z Peti Group Photo (JOI21_ho_t3) C++14
100 / 100
323 ms 134224 KB
#include <bits/stdc++.h>

using namespace std;

const int maxn = 5000;

int utan[maxn][maxn], ert[maxn][maxn];

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    int n;
    cin>>n;
    vector<int> v(n), inv(n);
    for(int i = 0; i < n; i++){
        cin>>v[i];
        --v[i];
        inv[v[i]] = i;
    }

    for(int i = 0; i < n; i++)
        for(int j = i-1; j >= 0; j--)
            utan[i][j] = utan[i][j+1] + (inv[i] < inv[j] ? 1 : 0);

    vector<int> db(n);
    iota(db.begin(), db.end(), 0);
    for(int i = 0; i < n; i++){
        ert[i][i] = db[inv[i]];
        for(int j = i+1; j < n; j++)
            ert[i][j] = ert[i][j-1] + db[inv[j]] - utan[j][i];
        for(int j = inv[i]+1; j < n; j++)
            db[j]--;
    }

    /*cout<<"ert:\n";
    for(int i = 0; i < n; i++){
        for(int j = i; j < n; j++)
            cout<<ert[i][j]<<' ';
        cout<<'\n';
    }*/

    vector<int> dp(n+1, (int)1e9);
    dp[0] = 0;
    for(int i = 1; i <= n; i++)
        for(int j = i; j > 0; j--)
            dp[i] = min(dp[i], dp[j-1] + ert[j-1][i-1]);
    cout<<dp[n]<<'\n';

    return 0;
}

/*
5
3 5 2 4 1
*/
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 328 KB Output is correct
4 Correct 0 ms 328 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 328 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 328 KB Output is correct
4 Correct 0 ms 328 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 328 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 468 KB Output is correct
12 Correct 1 ms 468 KB Output is correct
13 Correct 1 ms 468 KB Output is correct
14 Correct 1 ms 468 KB Output is correct
15 Correct 0 ms 468 KB Output is correct
16 Correct 1 ms 468 KB Output is correct
17 Correct 1 ms 468 KB Output is correct
18 Correct 1 ms 468 KB Output is correct
19 Correct 1 ms 468 KB Output is correct
20 Correct 1 ms 468 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 328 KB Output is correct
4 Correct 0 ms 328 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 328 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 468 KB Output is correct
12 Correct 1 ms 468 KB Output is correct
13 Correct 1 ms 468 KB Output is correct
14 Correct 1 ms 468 KB Output is correct
15 Correct 0 ms 468 KB Output is correct
16 Correct 1 ms 468 KB Output is correct
17 Correct 1 ms 468 KB Output is correct
18 Correct 1 ms 468 KB Output is correct
19 Correct 1 ms 468 KB Output is correct
20 Correct 1 ms 468 KB Output is correct
21 Correct 2 ms 2004 KB Output is correct
22 Correct 2 ms 2004 KB Output is correct
23 Correct 2 ms 1992 KB Output is correct
24 Correct 2 ms 2004 KB Output is correct
25 Correct 2 ms 2004 KB Output is correct
26 Correct 1 ms 1992 KB Output is correct
27 Correct 2 ms 1988 KB Output is correct
28 Correct 1 ms 2004 KB Output is correct
29 Correct 1 ms 2004 KB Output is correct
30 Correct 1 ms 2004 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 328 KB Output is correct
4 Correct 0 ms 328 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 328 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 468 KB Output is correct
12 Correct 1 ms 468 KB Output is correct
13 Correct 1 ms 468 KB Output is correct
14 Correct 1 ms 468 KB Output is correct
15 Correct 0 ms 468 KB Output is correct
16 Correct 1 ms 468 KB Output is correct
17 Correct 1 ms 468 KB Output is correct
18 Correct 1 ms 468 KB Output is correct
19 Correct 1 ms 468 KB Output is correct
20 Correct 1 ms 468 KB Output is correct
21 Correct 2 ms 2004 KB Output is correct
22 Correct 2 ms 2004 KB Output is correct
23 Correct 2 ms 1992 KB Output is correct
24 Correct 2 ms 2004 KB Output is correct
25 Correct 2 ms 2004 KB Output is correct
26 Correct 1 ms 1992 KB Output is correct
27 Correct 2 ms 1988 KB Output is correct
28 Correct 1 ms 2004 KB Output is correct
29 Correct 1 ms 2004 KB Output is correct
30 Correct 1 ms 2004 KB Output is correct
31 Correct 7 ms 9172 KB Output is correct
32 Correct 7 ms 9160 KB Output is correct
33 Correct 7 ms 9172 KB Output is correct
34 Correct 8 ms 9164 KB Output is correct
35 Correct 6 ms 9256 KB Output is correct
36 Correct 7 ms 9172 KB Output is correct
37 Correct 8 ms 9212 KB Output is correct
38 Correct 7 ms 9188 KB Output is correct
39 Correct 6 ms 9172 KB Output is correct
40 Correct 6 ms 9172 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 328 KB Output is correct
4 Correct 0 ms 328 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 328 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 468 KB Output is correct
12 Correct 1 ms 468 KB Output is correct
13 Correct 1 ms 468 KB Output is correct
14 Correct 1 ms 468 KB Output is correct
15 Correct 0 ms 468 KB Output is correct
16 Correct 1 ms 468 KB Output is correct
17 Correct 1 ms 468 KB Output is correct
18 Correct 1 ms 468 KB Output is correct
19 Correct 1 ms 468 KB Output is correct
20 Correct 1 ms 468 KB Output is correct
21 Correct 2 ms 2004 KB Output is correct
22 Correct 2 ms 2004 KB Output is correct
23 Correct 2 ms 1992 KB Output is correct
24 Correct 2 ms 2004 KB Output is correct
25 Correct 2 ms 2004 KB Output is correct
26 Correct 1 ms 1992 KB Output is correct
27 Correct 2 ms 1988 KB Output is correct
28 Correct 1 ms 2004 KB Output is correct
29 Correct 1 ms 2004 KB Output is correct
30 Correct 1 ms 2004 KB Output is correct
31 Correct 7 ms 9172 KB Output is correct
32 Correct 7 ms 9160 KB Output is correct
33 Correct 7 ms 9172 KB Output is correct
34 Correct 8 ms 9164 KB Output is correct
35 Correct 6 ms 9256 KB Output is correct
36 Correct 7 ms 9172 KB Output is correct
37 Correct 8 ms 9212 KB Output is correct
38 Correct 7 ms 9188 KB Output is correct
39 Correct 6 ms 9172 KB Output is correct
40 Correct 6 ms 9172 KB Output is correct
41 Correct 294 ms 134108 KB Output is correct
42 Correct 318 ms 134176 KB Output is correct
43 Correct 298 ms 134192 KB Output is correct
44 Correct 315 ms 134108 KB Output is correct
45 Correct 295 ms 134140 KB Output is correct
46 Correct 298 ms 134156 KB Output is correct
47 Correct 291 ms 134212 KB Output is correct
48 Correct 307 ms 134152 KB Output is correct
49 Correct 287 ms 134096 KB Output is correct
50 Correct 300 ms 134092 KB Output is correct
51 Correct 286 ms 134108 KB Output is correct
52 Correct 303 ms 134092 KB Output is correct
53 Correct 323 ms 134224 KB Output is correct
54 Correct 306 ms 134092 KB Output is correct
55 Correct 288 ms 134176 KB Output is correct