#include <bits/stdc++.h>
using namespace std;
#define int long long
#define fi first
#define se second
int t, n, m, k, ans = 1e18, a[1000005], pfx[1000005], sfx[1000005], tree[1000005], mod = 1e9 + 7;
set<int> s;
map<int, int> mp;
mt19937_64 rng;
int mul(int x, int y){
if(y == 0) return 1;
int ans = mul(x, y / 2);
if(y % 2 == 0) return (ans * ans) % mod;
else return (((ans * ans) % mod) * x) % mod;
}
void update(int x, int y){
while(x){
tree[x] += y; x -= (x & -x);
}
}
int get(int x){
int res = 0;
while(x <= n){
res += tree[x]; x += (x & -x);
}
return res;
}
void solve(){
int cnt = 0, mx = 0;
for(auto i: s) mp[i] = ++cnt;
for(int i = 1; i <= n; i++){
a[i] = mp[a[i]];
pfx[i] = pfx[i - 1] + get(a[i] + 1);
update(a[i], 1);
}
memset(tree, 0, sizeof(tree));
for(int i = n; i >= 1; i--){
sfx[i] = sfx[i + 1] + get(a[i] + 1);
update(a[i], 1);
}
for(int i = 1; i <= n; i++){
mx = max(mx, a[i]); update(a[i], -1);
int val = pfx[i] + sfx[i + 1] + get(mx + 1);
ans = min(ans, val);
}
cout << ans << "\n";
}
signed main(){
ios_base::sync_with_stdio(NULL); cin.tie(nullptr); cout.tie(nullptr);
rng.seed((int)main ^ time(0));
#ifdef Kawaii
auto starttime = chrono::high_resolution_clock::now();
#endif
cin >> n;
for(int i = 1; i <= n; i++){
cin >> a[i]; s.insert(a[i]);
}
solve();
#ifdef Kawaii
auto endtime = chrono::high_resolution_clock::now();
auto duration = chrono::duration_cast<chrono::milliseconds>(endtime - starttime).count();
cout << "\n=====" << "\nUsed: " << duration << " ms\n";
#endif
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
8148 KB |
Output is correct |
2 |
Correct |
4 ms |
8148 KB |
Output is correct |
3 |
Correct |
4 ms |
8148 KB |
Output is correct |
4 |
Correct |
4 ms |
8148 KB |
Output is correct |
5 |
Correct |
3 ms |
8148 KB |
Output is correct |
6 |
Correct |
3 ms |
8148 KB |
Output is correct |
7 |
Correct |
3 ms |
8148 KB |
Output is correct |
8 |
Correct |
4 ms |
8148 KB |
Output is correct |
9 |
Correct |
4 ms |
8148 KB |
Output is correct |
10 |
Incorrect |
3 ms |
8148 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
8148 KB |
Output is correct |
2 |
Correct |
3 ms |
8148 KB |
Output is correct |
3 |
Correct |
4 ms |
8148 KB |
Output is correct |
4 |
Correct |
3 ms |
8160 KB |
Output is correct |
5 |
Correct |
4 ms |
8148 KB |
Output is correct |
6 |
Correct |
4 ms |
8148 KB |
Output is correct |
7 |
Correct |
4 ms |
8148 KB |
Output is correct |
8 |
Correct |
3 ms |
8148 KB |
Output is correct |
9 |
Incorrect |
3 ms |
8148 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
4 ms |
8148 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
15 ms |
9508 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |