#include <bits/stdc++.h>
using namespace std;
#define REP(i,n) for(int (i)=0;(i)<(int)(n);(i)++)
#define REPO(i,n) for(int (i)=1; (i)<=(int)(n); (i)++)
#define SZ(v) ((int)(v).size())
#define ALL(v) (v).begin(),(v).end()
#define one first
#define two second
typedef long long ll;
typedef pair<int, int> pi;
const int INF = 0x3f2f1f0f;
const ll LINF = 1ll * INF * INF;
const int MAX_N = 3e5 + 100;
struct BIT {
int val[MAX_N];
BIT() {
for(int i=0; i<MAX_N; i++) val[i] = 0;
}
void update(int v, int k) {
for(v++; v<MAX_N; v+=v&-v) val[v] += k;
}
int getSum(int v) {
int res = 0;
for(v++; v; v-=v&-v) res += val[v];
return res;
}
int getSum(int a, int b) {
return getSum(b) - getSum(a-1);
}
};
int N, Nr[MAX_N];
ll CntL[MAX_N], CntR[MAX_N];
vector<int> Co;
int main() {
cin >> N; REPO(i, N) scanf("%d", &Nr[i]), Co.push_back(Nr[i]);
sort(ALL(Co)); Co.erase(unique(ALL(Co)), Co.end());
int cN = SZ(Co);
for(int i=1; i<=N; i++) Nr[i] = lower_bound(ALL(Co), Nr[i]) - Co.begin();
BIT bitL, bitR;
for(int i=1; i<=N; i++) {
CntL[i] = CntL[i-1] + bitL.getSum(Nr[i]+1, cN-1);
bitL.update(Nr[i], 1);
}
for(int i=N; i>=1; i--) {
CntR[i] = CntR[i+1] + bitR.getSum(Nr[i]+1, cN-1);
bitR.update(Nr[i], 1);
}
ll ans = LINF;
for(int i=0; i<=N; i++) ans = min(ans, CntL[i] + CntR[i+1]);
printf("%lld\n", ans);
return 0;
}
Compilation message
growing.cpp: In function 'int main()':
growing.cpp:39:63: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
cin >> N; REPO(i, N) scanf("%d", &Nr[i]), Co.push_back(Nr[i]);
^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
10100 KB |
Output is correct |
2 |
Correct |
0 ms |
10104 KB |
Output is correct |
3 |
Correct |
0 ms |
10104 KB |
Output is correct |
4 |
Correct |
0 ms |
10104 KB |
Output is correct |
5 |
Correct |
0 ms |
10100 KB |
Output is correct |
6 |
Correct |
0 ms |
10100 KB |
Output is correct |
7 |
Correct |
0 ms |
10100 KB |
Output is correct |
8 |
Correct |
0 ms |
10100 KB |
Output is correct |
9 |
Correct |
0 ms |
10104 KB |
Output is correct |
10 |
Correct |
0 ms |
10104 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
10100 KB |
Output is correct |
2 |
Correct |
0 ms |
10104 KB |
Output is correct |
3 |
Correct |
0 ms |
10104 KB |
Output is correct |
4 |
Correct |
0 ms |
10100 KB |
Output is correct |
5 |
Correct |
0 ms |
10100 KB |
Output is correct |
6 |
Correct |
0 ms |
10104 KB |
Output is correct |
7 |
Correct |
0 ms |
10100 KB |
Output is correct |
8 |
Correct |
0 ms |
10100 KB |
Output is correct |
9 |
Correct |
0 ms |
10104 KB |
Output is correct |
10 |
Correct |
0 ms |
10104 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
10104 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
23 ms |
10568 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |