QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#142303 | #1363. Bitonic Ordering | Anwar_Gehad_Maghraby# | WA | 2ms | 4700kb | C++23 | 1.3kb | 2023-08-18 22:16:53 | 2023-08-18 22:16:55 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 1000;
int Tree[N];
int query(int r){
int ans= 0;
for(; r > 0; r -= (r & (-r))) ans += Tree[r];
return ans;
}
void update(int i, int v){
for(; i <= N; i += (i & (-i))) Tree[i] += v;
}
int main() {
cin.tie(0);cin.sync_with_stdio(0);
cout.tie(0);cout.sync_with_stdio(0);
int n; cin>>n;
int a[n];
long long L[n], R[n];
vector<int> v;
map<int,int> mp;
for(int i = 0; i < n; i++){
cin>>a[i];
v.push_back(a[i]);
}
if(n == 1){
cout<<"0\n";
return 0;
}
sort(v.begin(), v.end());
for(int i = 0; i < n; i++){
mp[v[i]]= i;
}
for(int i = 0; i < n; i++){
a[i]= mp[a[i]] + 1;
}
long long inv = 0;
for(int i = 0; i < n; i++){
inv += query(N - 1) - query(a[i]);
L[i]= inv;
update(a[i], 1);
}
inv = 0;
memset(Tree, 0, sizeof Tree);
for(int i = n - 1; i >= 0; i--){
inv += query(N - 1) - query(a[i]);
R[i]= inv;
update(a[i], 1);
}
long long ans = LLONG_MAX;
for(int i = 0; i + 1 < n; i++){
ans= min(ans, L[i] + R[i + 1]);
}
cout<<ans<<"\n";
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 2ms
memory: 4700kb
input:
13 39 40 32 100 13 16 15 28 27 26 25 24 23
output:
15
result:
wrong answer 1st lines differ - expected: '14', found: '15'