QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#331396#1363. Bitonic Orderingweilaifuture#WA 3ms16088kbC++141.8kb2024-02-18 07:32:022024-02-18 07:32:03

Judging History

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

  • [2024-02-18 07:32:03]
  • 评测
  • 测评结果:WA
  • 用时:3ms
  • 内存:16088kb
  • [2024-02-18 07:32:02]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
#define int long long

int m1, m, tree[1200005], fr[300005], ba[300005];
int n, x, tmp;
vector <pair <int, int> > v;

bool cmp (pair <int, int> a, pair <int, int> b) {
    return a.first > b.first;
}

int lowbit(int x){return x&(-x);}

void update(int x, int v){
    while(x<=n) tree[x]+=v,x+=lowbit(x);
}

int query(int x){
    int tmp1=0;
    while(x) tmp1+=tree[x], x-=lowbit(x);
    return tmp1;
}

signed main() {
    cin >> n;
    for (int i = 1; i <= n; i++) {
        cin >> x;
        v.push_back({x, i});
        if (x > m1) {
            m1 = x;
            m = i;
        }
    } sort(v.begin(), v.end(), cmp);

    memset(tree, 0, sizeof(tree));
    for (auto g:v) {
        int a = g.first;
        int idx = g.second;

        if (idx == m) continue;
        fr[idx] = query(idx - 1);
        update(idx,1); tmp++;
    }
    
    for(auto g:v) g.first = -g.first; 
    sort(v.begin(), v.end(), cmp);
    memset(tree, 0, sizeof(tree)); tmp=0;
    for (auto g:v) {
        int a = g.first;
        int idx = g.second;

        if (idx == m) continue;
        ba[idx] = tmp - query(idx);
        update(idx,1); tmp++;
    }


    for (int i = 1; i <= n; i++) {
        fr[i] += fr[i - 1];
    }

    for (int i = n; i > 0; i--) {
        ba[i] += ba[i + 1];
    }

    int ans = 1e18;

    for(int i=1;i<=n+1;i++) ans = min(ans,fr[i-1]+ba[i]+abs(i-m));

    /*for (int i = 1; i < m; i++) {
        int tmp = fr[i - 1] + ba[i] + abs(i - m);
        //cout << fr[i - 1] << " " << ba[i] << endl;

        ans = min(tmp, ans);
    }
    ans = min(ans, fr[m - 1] + ba[m + 1]);
    for (int i = m + 1; i <= n; i++) {
        int tmp = fr[i] + ba[i + 1] + abs(i - m);
        ans = min(tmp, ans);
    }*/
    cout << ans << endl;
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 3ms
memory: 16088kb

input:

13
39
40
32
100
13
16
15
28
27
26
25
24
23

output:

16

result:

wrong answer 1st lines differ - expected: '14', found: '16'