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>
const long long N = 3e5 +5;
const long long inf = 1e9;
using namespace std;
typedef pair <long long, long long> ii;
vector <ii> mv;
long long n, h, res, bit[N], cnt, pos[N], dp[N];
bool check[N];
void update(long long i){
while (i <= n){
bit[i]++;
i += i&(-i);
}
}
long long get(long long i){
long long ans = 0, z = i;
while (i > 0){
ans += bit[i];
i -= i&(-i);
}
return z - ans;
}
int main(){
ios::sync_with_stdio(false);
cin >> n;
for (long long i = 1; i <= n; i++){
cin >> h;
mv.push_back(ii(h, i));
}
sort(mv.begin(), mv.end());
mv.push_back(ii(-inf, -inf));
for (long long i = 0; i < n; i++){
long long cnt = i, z = mv[cnt].first, minn = inf, sum = 0;
while (mv[cnt].first == z) {
pos[cnt] = get(mv[cnt].second);
cnt++;
}
dp[cnt] = 0;
for (long long j = cnt-1; j >= i; j--){
dp[j] = dp[j+1] + n-i-pos[j]-cnt+j+1;
}
minn = dp[i];
for (long long j = i; j < cnt; j++){
sum += pos[j]-1-j+i;
minn = min (minn, dp[j+1] + sum);
update(mv[j].second);
}
res += minn;
i = cnt-1;
}
cout << res;
}
# | 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... |