This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
#include <queue>
using namespace std;
int n;
int const inf = 1e9;
vector<int>a, idx;
vector<int>cost;
int total_cost;
void calculate_cost(int val){
vector<int>p(n+1);
iota(p.begin(), p.end(), 0);
reverse(p.begin()+1, p.begin()+val+1);
for(int i = val; i >= 1; i--){
int inv = 0;
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]))
inv++;
}
cost.push_back(inv);
total_cost += inv;
}
}
int main(){
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;
}
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] + total_cost);
total_cost -= cost.back();
cost.pop_back();
}
}
cout << dp[n] << '\n';
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |