QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#208311 | #1363. Bitonic Ordering | rlong | WA | 0ms | 3576kb | C++14 | 2.8kb | 2023-10-09 13:29:26 | 2023-10-09 13:29:26 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int tree[300005];
int n;
int sum(int k) {
int s = 0;
while (k >= 1) {
s += tree[k];
k -= k&-k;
}
return s;
}
void add(int k, int x) {
while (k <= n) {
tree[k] += x;
k += k&-k;
}
}
int main() {
cin >> n;
map<int, int> mp;
int in[n+5];
int arr[n+5]; // 1-N relative order
int brr[n+5]; // backwards
for(int i=1;i<=n;i++) {
cin >> in[i];
mp[in[i]] = i;
}
sort(in+1,in+1+n);
for(int i=1;i<=n;i++) {
arr[mp[in[i]]] = i;
}
int maxpos = 0;
for(int i=1;i<=n;i++) {
if(arr[i] == n) maxpos = i;
}
// cout << "maxpos = " << maxpos << endl;
for(int i=maxpos;i<=n-1;i++) {
arr[i] = arr[i+1];
}
for(int i=1;i<=n-1;i++) {
brr[i] = arr[n-i];
}
/*
for(int i=1;i<=n-1;i++) {
cout << arr[i] << " ";
}
cout << endl;
*/
ll great_a[n+5];
great_a[1] = 0;
add(arr[1], 1);
for(int i=2;i<=n-1;i++) {
great_a[i] = (i-1) - sum(arr[i]);
add(arr[i], 1);
}
for(int i=0;i<=n+2;i++) tree[i] = 0;
ll great_b[n+5];
great_b[1] = 0;
add(brr[1], 1);
for(int i=2;i<=n-1;i++) {
great_b[i] = (i-1) - sum(brr[i]);
add(brr[i], 1);
}
/*
for(int i=1;i<=n-1;i++) {
cout << great_a[i] << " ";
}
cout << endl;
for(int i=1;i<=n-1;i++) {
cout << great_b[i] << " ";
}
cout << endl;
*/
for(int i=2;i<=n-1;i++) {
great_a[i] += great_a[i-1];
great_b[i] += great_b[i-1];
}
long long minm = 99999999999999999LL;
for(int i=0;i<=n-1;i++) { // i things in front of the MAX
if(i == 0) {
minm = min(minm, max(maxpos - (i+1), (i+1) - maxpos)
+ great_b[n-(i+1)] );
}
else if(i == n-1) {
minm = min(minm, max(maxpos - (i+1), (i+1) - maxpos)
+ great_a[i]);
}
else {
minm = min(minm, max(maxpos - (i+1), (i+1) - maxpos)
+ great_a[i]
+ great_b[n-(i+1)]);
}
/*
if(minm == 5) {
cout << "i = " << i << endl;
cout << max(maxpos - (i+1), (i+1) - maxpos) << endl;
cout << great_a[i] << endl;
cout << great_b[n-(i+1)] << endl;
break;
}
*/
}
cout << minm << endl;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 0ms
memory: 3576kb
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'