QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#331389 | #1363. Bitonic Ordering | weilaifuture# | WA | 3ms | 16412kb | C++14 | 2.0kb | 2024-02-18 07:20:50 | 2024-02-18 07:20:50 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define int long long
int m1, m, tree[1200005], fr[300005], ba[300005];
int n, x;
vector <pair <int, int> > v;
bool cmp (pair <int, int> a, pair <int, int> b) {
return a.first > b.first;
}
void update(int idx, int ki, int ka, int x) {
//cout << ki << " " << ka << " " << idx << endl;
if (ki > ka || ki > x || ka < x) return;
if (ki == ka && ki == x) {
tree[idx]++;
return;
}
update(idx * 2, ki, (ki + ka)/2, x);
update(idx * 2 + 1, (ki + ka)/2 + 1, ka, x);
tree[idx] = tree[idx * 2] + tree[idx * 2 + 1];
}
int query(int idx, int ki, int ka, int qki, int qka) {
if (ki > ka || ki > qka || qki > ka) return 0;
if (qki <= ki && ka <= qka) return tree[idx];
int a = query(idx * 2, ki, (ki + ka)/2, qki, qka);
int b = query(idx * 2 + 1, (ki + ka)/2 + 1, ka, qki, qka);
return a + b;
}
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(1, 1, n, 1, idx - 1);
ba[idx] = query(1, 1, n, idx + 1, n);
update(1, 1, n, idx);
}
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: 16412kb
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'