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<bits/stdc++.h>
using namespace std;
const int N = 5010;
int n;
int v[N];
int dp[N];
struct Segtree{
int tree[4*N];
int join(int a, int b){
return a+b;
}
void build(int node, int l, int r){
if(l == r){
tree[node] = 0;
return;
}
int mid = (l+r)/2;
build(2*node, l, mid);
build(2*node+1, mid+1, r);
tree[node] = join(tree[2*node], tree[2*node+1]);
return;
}
void update(int node, int l, int r, int pos, int val){
if(l == r){
tree[node] = val;
return;
}
int mid = (l+r)/2;
if(l <= pos and pos <= mid) update(2*node, l, mid, pos, val);
else update(2*node+1, mid+1,r, pos, val);
tree[node] = join(tree[2*node], tree[2*node+1]);
return;
}
int query(int node, int l, int r, int tl, int tr){
if(l > r) return 0;
if(l <= tl and tr <= r) return tree[node];
if(l > tr or tl > r) return 0;
int mid = (tl+tr)/2;
return join(query(2*node, l, r, tl, mid), query(2*node+1, l, r, mid+1, tr));
}
} seg;
int p[N];
void solve(int t){
vector <int> atual;
for(int i = 1;i <= n;i++){
if(v[i] <= t) atual.push_back(v[i]);
}
for(int i = 1;i <= t;i++){
p[atual[i-1]] = i;
}
seg.build(1, 1, t);
int sum = 0;
dp[t] = 1e8;
for(int i = t;i > 0;i--){
sum += t - p[i];
sum -= seg.query(1, 1, p[i]-1, 1, t);
seg.update(1, 1, t, p[i], 1);
dp[t] = min(dp[t], dp[i-1] + sum);
}
return;
}
int main(){
cin >> n;
for(int i = 1;i <= n;i++){
cin >> v[i];
}
dp[0] = 0;
for(int i = 1;i <= n;i++){
solve(i);
}
cout << dp[n] << '\n';
}
# | 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... |