Submission #1091189

# Submission time Handle Problem Language Result Execution time Memory
1091189 2024-09-20T04:30:06 Z yogesh_sane Group Photo (JOI21_ho_t3) C++17
100 / 100
3669 ms 135248 KB
#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>

using namespace std;
 
int n;
int const inf = 1e9;
vector<int>a, idx;
vector<int>cost;
int total_cost;
vector<vector<int>>left_num, right_num;
void precalc_cost(){
    left_num.resize(n+1);
    right_num.resize(n+1);
    for(int i = 1; i <= n; i++){
        for(int j = i+1; j <=n; j++){
            left_num[a[j]].push_back(a[i]);
        }
    }
    for(int i = n; i >= 1; i--){
        for(int j = i-1; j >= 1; j--){
            right_num[a[j]].push_back(a[i]);
        }
    }
    for(int i = 1; i <= n; i++){
        sort(left_num[i].begin(), left_num[i].end());
        sort(right_num[i].begin(), right_num[i].end());
    }
}
int query(vector<int> &v, int l, int r){
    if(l > r)
        return 0;
    //numbers less than or equal to r
    int num_r = lower_bound(v.begin(), v.end(), r+1)-v.begin();
    //numbers less than l
    int num_l = lower_bound(v.begin(), v.end(),l)-v.begin();
    return num_r-num_l;
}
int query_left(int num, int l, int r){
    return query(left_num[num], l, r);
}
int query_right(int num, int l, int r){
    return query(right_num[num], l, r);
}
void calculate_cost(int val){
    vector<int>p(n+1);
    iota(p.begin(), p.end(), 0);
    reverse(p.begin()+1, p.begin()+val+1);
    int inv = 0;
    for(int i = val; i >= 1; i--){
        inv += query_left(i, val+1, n);
        inv += query_right(i, i+1, val);
        cost.push_back(inv);
        total_cost += inv;
    }
}
 
int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin >> n;
    a.resize(n+1);
    idx.resize(n+1);
    vector<int>dp(n+1, inf);
    for(int i = 1; i <= n; i++){
        cin >> a[i];
        idx[a[i]] = i;
    }
    precalc_cost();
    dp[0] = 0;
    for(int i = 1; i <= n; i++){
        calculate_cost(i);
        for(int j = 1; j <= i; j++){
            dp[i] = min(dp[i], dp[j-1] + cost.back());
            cost.pop_back();
        }
    }
    cout << dp[n] << '\n';
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 3 ms 604 KB Output is correct
22 Correct 3 ms 604 KB Output is correct
23 Correct 3 ms 604 KB Output is correct
24 Correct 3 ms 604 KB Output is correct
25 Correct 3 ms 604 KB Output is correct
26 Correct 4 ms 604 KB Output is correct
27 Correct 3 ms 604 KB Output is correct
28 Correct 3 ms 716 KB Output is correct
29 Correct 3 ms 600 KB Output is correct
30 Correct 3 ms 604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 3 ms 604 KB Output is correct
22 Correct 3 ms 604 KB Output is correct
23 Correct 3 ms 604 KB Output is correct
24 Correct 3 ms 604 KB Output is correct
25 Correct 3 ms 604 KB Output is correct
26 Correct 4 ms 604 KB Output is correct
27 Correct 3 ms 604 KB Output is correct
28 Correct 3 ms 716 KB Output is correct
29 Correct 3 ms 600 KB Output is correct
30 Correct 3 ms 604 KB Output is correct
31 Correct 60 ms 4220 KB Output is correct
32 Correct 63 ms 4184 KB Output is correct
33 Correct 61 ms 4184 KB Output is correct
34 Correct 61 ms 4188 KB Output is correct
35 Correct 60 ms 4184 KB Output is correct
36 Correct 65 ms 4184 KB Output is correct
37 Correct 60 ms 4184 KB Output is correct
38 Correct 60 ms 4184 KB Output is correct
39 Correct 61 ms 4184 KB Output is correct
40 Correct 60 ms 4188 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 3 ms 604 KB Output is correct
22 Correct 3 ms 604 KB Output is correct
23 Correct 3 ms 604 KB Output is correct
24 Correct 3 ms 604 KB Output is correct
25 Correct 3 ms 604 KB Output is correct
26 Correct 4 ms 604 KB Output is correct
27 Correct 3 ms 604 KB Output is correct
28 Correct 3 ms 716 KB Output is correct
29 Correct 3 ms 600 KB Output is correct
30 Correct 3 ms 604 KB Output is correct
31 Correct 60 ms 4220 KB Output is correct
32 Correct 63 ms 4184 KB Output is correct
33 Correct 61 ms 4184 KB Output is correct
34 Correct 61 ms 4188 KB Output is correct
35 Correct 60 ms 4184 KB Output is correct
36 Correct 65 ms 4184 KB Output is correct
37 Correct 60 ms 4184 KB Output is correct
38 Correct 60 ms 4184 KB Output is correct
39 Correct 61 ms 4184 KB Output is correct
40 Correct 60 ms 4188 KB Output is correct
41 Correct 3504 ms 134992 KB Output is correct
42 Correct 3561 ms 135168 KB Output is correct
43 Correct 3598 ms 135212 KB Output is correct
44 Correct 3527 ms 135208 KB Output is correct
45 Correct 3530 ms 135212 KB Output is correct
46 Correct 3551 ms 135076 KB Output is correct
47 Correct 3549 ms 134992 KB Output is correct
48 Correct 3597 ms 135248 KB Output is correct
49 Correct 3533 ms 134996 KB Output is correct
50 Correct 3559 ms 135216 KB Output is correct
51 Correct 3523 ms 135224 KB Output is correct
52 Correct 3590 ms 134996 KB Output is correct
53 Correct 3510 ms 134992 KB Output is correct
54 Correct 3524 ms 135248 KB Output is correct
55 Correct 3669 ms 135248 KB Output is correct