#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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
376 KB |
Output is correct |
2 |
Correct |
1 ms |
376 KB |
Output is correct |
3 |
Correct |
1 ms |
540 KB |
Output is correct |
4 |
Correct |
1 ms |
652 KB |
Output is correct |
5 |
Correct |
1 ms |
652 KB |
Output is correct |
6 |
Correct |
1 ms |
652 KB |
Output is correct |
7 |
Correct |
1 ms |
652 KB |
Output is correct |
8 |
Correct |
1 ms |
652 KB |
Output is correct |
9 |
Correct |
2 ms |
772 KB |
Output is correct |
10 |
Correct |
1 ms |
772 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
772 KB |
Output is correct |
2 |
Correct |
2 ms |
772 KB |
Output is correct |
3 |
Correct |
1 ms |
772 KB |
Output is correct |
4 |
Correct |
1 ms |
772 KB |
Output is correct |
5 |
Correct |
1 ms |
788 KB |
Output is correct |
6 |
Correct |
1 ms |
788 KB |
Output is correct |
7 |
Correct |
1 ms |
788 KB |
Output is correct |
8 |
Correct |
1 ms |
788 KB |
Output is correct |
9 |
Correct |
1 ms |
868 KB |
Output is correct |
10 |
Correct |
1 ms |
868 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
868 KB |
Output is correct |
2 |
Correct |
2 ms |
928 KB |
Output is correct |
3 |
Correct |
2 ms |
940 KB |
Output is correct |
4 |
Correct |
3 ms |
1100 KB |
Output is correct |
5 |
Correct |
3 ms |
1284 KB |
Output is correct |
6 |
Correct |
2 ms |
1284 KB |
Output is correct |
7 |
Correct |
3 ms |
1364 KB |
Output is correct |
8 |
Correct |
3 ms |
1512 KB |
Output is correct |
9 |
Correct |
3 ms |
1512 KB |
Output is correct |
10 |
Correct |
3 ms |
1512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
3852 KB |
Output is correct |
2 |
Correct |
31 ms |
6380 KB |
Output is correct |
3 |
Correct |
50 ms |
9536 KB |
Output is correct |
4 |
Correct |
70 ms |
13484 KB |
Output is correct |
5 |
Correct |
42 ms |
15348 KB |
Output is correct |
6 |
Correct |
27 ms |
15348 KB |
Output is correct |
7 |
Correct |
84 ms |
20816 KB |
Output is correct |
8 |
Correct |
96 ms |
23708 KB |
Output is correct |
9 |
Correct |
97 ms |
26688 KB |
Output is correct |
10 |
Correct |
103 ms |
29556 KB |
Output is correct |