답안 #535088

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
535088 2022-03-09T11:38:25 Z Cookie197 Group Photo (JOI21_ho_t3) C++17
100 / 100
1034 ms 201412 KB
#include<iostream>
#include<algorithm>
#include<vector>
#include<set>

using namespace std;
#define ll long long
#define pii pair<ll,ll> 
#define mp make_pair
#define endl "\n"
#define out(x) cout<< #x << " = " << x << endl
#define outp(x) cout << #x << " first = " << x.first << " second = " << x.second << endl
#define inf 3e18
int n;
int arr[5005], pos[5005];

int a[5005][5005]; // 預處理,[i,i+1, ... j] 這個區間放在正確位置,會出現幾個逆序
int arev[5005][5005];
int b[5005][5005]; // 預處理,前i個自排列[舊的],第i+1到j個為-1的遞減[新的],舊的和新的之間會產生幾個逆序

int dp[5005];

vector<int> bit(5005);
void init(){
    for (int i=1;i<=n;i++) bit[i] = 0;
}
void add(int id,int x){
    for (int i=id;i<=n;i+=(i&-i)) bit[i] += x;
}
int sum(int id){
    if (id==0) return 0;
    int ans = 0;
    for (int i=id;i>=1;i-=(i&-i)) ans += bit[i];
    return ans;
}

signed main(){
    ios::sync_with_stdio(false); cin.tie(0);
    cin>>n;
    for (int i=1;i<=n;i++) cin>>arr[i], pos[arr[i]] = i;
    
    // precalc a
    for (int i=1;i<=n;i++){
        init();
        for (int j=i;j<=n;j++){
            a[i][j] = a[i][j-1] + sum(n) - sum(pos[j]-1);
            arev[i][j] = (j-i)*(j-i+1)/2 - a[i][j];
            add(pos[j],1);
        }
    }

    // precalc b
    for (int i=1;i<=n;i++){
        init();
        for (int z=1;z<=i;z++) add(pos[z],1);
        for (int j=i+1;j<=n;j++){
            b[i][j] = b[i][j-1] + i - sum(pos[j]-1);
        }
    }

    // dp[i] = 目前看到前i個位置,逆序最小數量
    for (int i=1;i<=n;i++){
        dp[i] = 1e9;
        for (int j=0;j<i;j++){
            dp[i] = min(dp[i], dp[j] + arev[j+1][i] + b[j][i]);
            //cout<<i+1<<" "<<j<<"   "<<arev[j+1][i]<<endl;
            //cout<<i<<" "<<j<<"   "<<b[j+1][i]<<endl;
        }

    }

    cout<<dp[n]<<endl;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 1 ms 348 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 332 KB Output is correct
8 Correct 1 ms 460 KB Output is correct
9 Correct 1 ms 460 KB Output is correct
10 Correct 1 ms 460 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 1 ms 348 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 332 KB Output is correct
8 Correct 1 ms 460 KB Output is correct
9 Correct 1 ms 460 KB Output is correct
10 Correct 1 ms 460 KB Output is correct
11 Correct 1 ms 460 KB Output is correct
12 Correct 1 ms 588 KB Output is correct
13 Correct 1 ms 588 KB Output is correct
14 Correct 1 ms 588 KB Output is correct
15 Correct 1 ms 588 KB Output is correct
16 Correct 1 ms 588 KB Output is correct
17 Correct 1 ms 588 KB Output is correct
18 Correct 1 ms 588 KB Output is correct
19 Correct 1 ms 588 KB Output is correct
20 Correct 1 ms 588 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 1 ms 348 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 332 KB Output is correct
8 Correct 1 ms 460 KB Output is correct
9 Correct 1 ms 460 KB Output is correct
10 Correct 1 ms 460 KB Output is correct
11 Correct 1 ms 460 KB Output is correct
12 Correct 1 ms 588 KB Output is correct
13 Correct 1 ms 588 KB Output is correct
14 Correct 1 ms 588 KB Output is correct
15 Correct 1 ms 588 KB Output is correct
16 Correct 1 ms 588 KB Output is correct
17 Correct 1 ms 588 KB Output is correct
18 Correct 1 ms 588 KB Output is correct
19 Correct 1 ms 588 KB Output is correct
20 Correct 1 ms 588 KB Output is correct
21 Correct 2 ms 2892 KB Output is correct
22 Correct 2 ms 2892 KB Output is correct
23 Correct 2 ms 2892 KB Output is correct
24 Correct 3 ms 3020 KB Output is correct
25 Correct 2 ms 2892 KB Output is correct
26 Correct 3 ms 3020 KB Output is correct
27 Correct 3 ms 3020 KB Output is correct
28 Correct 3 ms 3020 KB Output is correct
29 Correct 3 ms 3020 KB Output is correct
30 Correct 3 ms 3020 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 1 ms 348 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 332 KB Output is correct
8 Correct 1 ms 460 KB Output is correct
9 Correct 1 ms 460 KB Output is correct
10 Correct 1 ms 460 KB Output is correct
11 Correct 1 ms 460 KB Output is correct
12 Correct 1 ms 588 KB Output is correct
13 Correct 1 ms 588 KB Output is correct
14 Correct 1 ms 588 KB Output is correct
15 Correct 1 ms 588 KB Output is correct
16 Correct 1 ms 588 KB Output is correct
17 Correct 1 ms 588 KB Output is correct
18 Correct 1 ms 588 KB Output is correct
19 Correct 1 ms 588 KB Output is correct
20 Correct 1 ms 588 KB Output is correct
21 Correct 2 ms 2892 KB Output is correct
22 Correct 2 ms 2892 KB Output is correct
23 Correct 2 ms 2892 KB Output is correct
24 Correct 3 ms 3020 KB Output is correct
25 Correct 2 ms 2892 KB Output is correct
26 Correct 3 ms 3020 KB Output is correct
27 Correct 3 ms 3020 KB Output is correct
28 Correct 3 ms 3020 KB Output is correct
29 Correct 3 ms 3020 KB Output is correct
30 Correct 3 ms 3020 KB Output is correct
31 Correct 26 ms 13728 KB Output is correct
32 Correct 22 ms 13772 KB Output is correct
33 Correct 23 ms 13720 KB Output is correct
34 Correct 22 ms 13768 KB Output is correct
35 Correct 22 ms 13752 KB Output is correct
36 Correct 23 ms 13728 KB Output is correct
37 Correct 22 ms 13764 KB Output is correct
38 Correct 28 ms 13740 KB Output is correct
39 Correct 27 ms 13692 KB Output is correct
40 Correct 27 ms 13716 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 1 ms 348 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 332 KB Output is correct
8 Correct 1 ms 460 KB Output is correct
9 Correct 1 ms 460 KB Output is correct
10 Correct 1 ms 460 KB Output is correct
11 Correct 1 ms 460 KB Output is correct
12 Correct 1 ms 588 KB Output is correct
13 Correct 1 ms 588 KB Output is correct
14 Correct 1 ms 588 KB Output is correct
15 Correct 1 ms 588 KB Output is correct
16 Correct 1 ms 588 KB Output is correct
17 Correct 1 ms 588 KB Output is correct
18 Correct 1 ms 588 KB Output is correct
19 Correct 1 ms 588 KB Output is correct
20 Correct 1 ms 588 KB Output is correct
21 Correct 2 ms 2892 KB Output is correct
22 Correct 2 ms 2892 KB Output is correct
23 Correct 2 ms 2892 KB Output is correct
24 Correct 3 ms 3020 KB Output is correct
25 Correct 2 ms 2892 KB Output is correct
26 Correct 3 ms 3020 KB Output is correct
27 Correct 3 ms 3020 KB Output is correct
28 Correct 3 ms 3020 KB Output is correct
29 Correct 3 ms 3020 KB Output is correct
30 Correct 3 ms 3020 KB Output is correct
31 Correct 26 ms 13728 KB Output is correct
32 Correct 22 ms 13772 KB Output is correct
33 Correct 23 ms 13720 KB Output is correct
34 Correct 22 ms 13768 KB Output is correct
35 Correct 22 ms 13752 KB Output is correct
36 Correct 23 ms 13728 KB Output is correct
37 Correct 22 ms 13764 KB Output is correct
38 Correct 28 ms 13740 KB Output is correct
39 Correct 27 ms 13692 KB Output is correct
40 Correct 27 ms 13716 KB Output is correct
41 Correct 1000 ms 201152 KB Output is correct
42 Correct 1014 ms 201200 KB Output is correct
43 Correct 1034 ms 201252 KB Output is correct
44 Correct 1033 ms 201284 KB Output is correct
45 Correct 1018 ms 201220 KB Output is correct
46 Correct 994 ms 201284 KB Output is correct
47 Correct 1025 ms 201240 KB Output is correct
48 Correct 987 ms 201412 KB Output is correct
49 Correct 1006 ms 201324 KB Output is correct
50 Correct 994 ms 201272 KB Output is correct
51 Correct 1012 ms 201324 KB Output is correct
52 Correct 1002 ms 201336 KB Output is correct
53 Correct 1006 ms 201332 KB Output is correct
54 Correct 1008 ms 201344 KB Output is correct
55 Correct 993 ms 201380 KB Output is correct