답안 #1091039

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1091039 2024-09-19T15:06:45 Z yogesh_sane Group Photo (JOI21_ho_t3) C++17
44 / 100
5000 ms 6740 KB
#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
#include <queue>
using namespace std;

int n;
vector<int>a, idx;
vector<int>p;
int const inf = 1e9;
vector<int>dp;
vector<vector<int>>dpcost;

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);
}
int cost(int pos, int val){
    if(dpcost[pos][val] != -1)
        return dpcost[pos][val];
    //cout << "cost(" << pos << ',' << val << ")\n";
    iota(p.begin(), p.end(), 0);
    reverse(p.begin()+pos, p.begin()+val+1);
    int inv = 0;
    for(int i = pos; i <= val; i++){
        if(i == pos){
            inv += query_left(i, val+1, n);
            inv += query_right(i, i+1, val);
        }else if(i == val){
            //inv += query_left(i, pos, i-1);
            inv += query_left(i, val+1, n);
        }else{
            //inv += query_left(i, pos, i-1);
            inv += query_left(i, val+1, n);
            inv += query_right(i, i+1, val);
            //inv += query_right(i, 1, pos-1);
        }
    }

    // int inv2 = 0;
    // for(int i = pos; i <= val; i++){
    //     for(int j = i+1; j <= n; j++){
    //         if((p[i] < p[j] && idx[i] > idx[j]) ||
    //            (p[i] > p[j] && idx[i] < idx[j]))
    //            inv2++;
    //     }
    // }
    //cout << inv << ' ' << inv2 << endl;
    return dpcost[pos][val] = inv;
}

int solve(int k){
    if(k > n)
        return 0;
    if(dp[k] != -1)
        return dp[k];
    //cout << "solve(" << k <<")" << endl;
    int ans = inf;    
    for(int i = k; i <= n; i++){
        ans = min(ans, cost(k, i) + solve(i+1));
    }
    return dp[k] = ans;
}
int main(){
    cin >> n;
    a.resize(n+1);
    p.resize(n+1);
    idx.resize(n+1);
    dp.resize(n+1, -1);
    dpcost.resize(n+1, vector<int>(n+1, -1));
    for(int i = 1; i <= n; i++){
        cin >> a[i];
        idx[a[i]] = i;
    }
    precalc_cost();
    cout << solve(1) << '\n';
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 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
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 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 1 ms 344 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 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 1 ms 344 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 91 ms 860 KB Output is correct
22 Correct 91 ms 860 KB Output is correct
23 Correct 94 ms 860 KB Output is correct
24 Correct 90 ms 856 KB Output is correct
25 Correct 91 ms 856 KB Output is correct
26 Correct 92 ms 856 KB Output is correct
27 Correct 90 ms 860 KB Output is correct
28 Correct 91 ms 856 KB Output is correct
29 Correct 92 ms 856 KB Output is correct
30 Correct 91 ms 856 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 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 1 ms 344 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 91 ms 860 KB Output is correct
22 Correct 91 ms 860 KB Output is correct
23 Correct 94 ms 860 KB Output is correct
24 Correct 90 ms 856 KB Output is correct
25 Correct 91 ms 856 KB Output is correct
26 Correct 92 ms 856 KB Output is correct
27 Correct 90 ms 860 KB Output is correct
28 Correct 91 ms 856 KB Output is correct
29 Correct 92 ms 856 KB Output is correct
30 Correct 91 ms 856 KB Output is correct
31 Execution timed out 5086 ms 6740 KB Time limit exceeded
32 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 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 1 ms 344 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 91 ms 860 KB Output is correct
22 Correct 91 ms 860 KB Output is correct
23 Correct 94 ms 860 KB Output is correct
24 Correct 90 ms 856 KB Output is correct
25 Correct 91 ms 856 KB Output is correct
26 Correct 92 ms 856 KB Output is correct
27 Correct 90 ms 860 KB Output is correct
28 Correct 91 ms 856 KB Output is correct
29 Correct 92 ms 856 KB Output is correct
30 Correct 91 ms 856 KB Output is correct
31 Execution timed out 5086 ms 6740 KB Time limit exceeded
32 Halted 0 ms 0 KB -