//#include "ramen.h"
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define fi first
#define se second
#define pb push_back
//#define mp make_pair
#define foru(i, l, r) for(int i = l; i <= r; i++)
#define ford(i, r, l) for(int i = r; i >= l; i--)
typedef pair<int, int> ii;
typedef pair<ii, int> iii;
typedef pair<ii, ii> iiii;
const int N = 4e5 + 5;
const int oo = 1e18 + 7, mod = 1e9 + 7;
int n, a[N];
int BIT[N];
void upd(int id, int val){
for(; id <= n; id += id & -id) BIT[id] += val;
}
int gett(int id){
int ans = 0;
for(; id; id -= id & -id) ans += BIT[id];
return ans;
}
int get(int l, int r){
return gett(r) - gett(l - 1);
}
map<int, vector<int>> mp;
void process(){
cin >> n;
for(int i = 1; i <= n; i++){
cin >> a[i];
mp[a[i]].pb(i);
//vc.pb({a[i], i});
}
//sort(vc.begin(), vc.end(), cmp);
for(int i = 1; i <= n; i++) upd(i, 1);
int answer = 0;
for(auto it : mp){
//cout << it.fi << "\n";
deque<int> dq;
vector<int> temp = it.se;
sort(temp.begin(), temp.end());
for(auto it : temp) dq.push_back(it);
while(!dq.empty()){
int pos1 = dq.front(), pos2 = dq.back();
int temp1 = get(1, pos1 - 1), temp2 = get(pos2 + 1, n);
//cout << temp1 << " " << temp2 << "\n";
if(temp1 < temp2){
upd(pos1, -1);
dq.pop_front();
}
else{
upd(pos2, -1);
dq.pop_back();
}
answer += min(temp1, temp2);
}
//cout << answer << "\n";
}
/*
for(auto it : vc){
while(vc[itr].fi > it.fi){
upd(vc[itr].se, 1);
itr++;
}
//cout << it.fi << " " << it.se << " " << itr << " " << get(it.se - 1) << " ""\n";
answer += min(get(it.se - 1), get(n) - get(it.se));
}*/
cout << answer;
}
signed main(){
ios_base::sync_with_stdio(0);
process();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
2 ms |
468 KB |
Output is correct |
4 |
Correct |
2 ms |
724 KB |
Output is correct |
5 |
Correct |
3 ms |
852 KB |
Output is correct |
6 |
Correct |
2 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
3 ms |
852 KB |
Output is correct |
9 |
Correct |
5 ms |
724 KB |
Output is correct |
10 |
Correct |
4 ms |
852 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
2168 KB |
Output is correct |
2 |
Correct |
101 ms |
12840 KB |
Output is correct |
3 |
Correct |
134 ms |
17388 KB |
Output is correct |
4 |
Correct |
218 ms |
25296 KB |
Output is correct |
5 |
Correct |
122 ms |
27172 KB |
Output is correct |
6 |
Correct |
59 ms |
13776 KB |
Output is correct |
7 |
Correct |
51 ms |
10324 KB |
Output is correct |
8 |
Correct |
191 ms |
40744 KB |
Output is correct |
9 |
Correct |
241 ms |
29064 KB |
Output is correct |
10 |
Correct |
247 ms |
40812 KB |
Output is correct |