QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#142303#1363. Bitonic OrderingAnwar_Gehad_Maghraby#WA 2ms4700kbC++231.3kb2023-08-18 22:16:532023-08-18 22:16:55

Judging History

你现在查看的是最新测评结果

  • [2023-08-18 22:16:55]
  • 评测
  • 测评结果:WA
  • 用时:2ms
  • 内存:4700kb
  • [2023-08-18 22:16:53]
  • 提交

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'