#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>0; 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];
vector<int> Co;
vector<int> List[MAX_N];
ll Ls[MAX_N], Rs[MAX_N];
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());
for(int i=1; i<=N; i++) Nr[i] = lower_bound(ALL(Co), Nr[i]) - Co.begin();
for(int i=1; i<=N; i++) List[Nr[i]].push_back(i);
int cN = SZ(Co);
BIT bit;
for(int i=1; i<=N; i++) bit.update(i, +1);
ll ans = 0;
for(int c=0; c<cN; c++) {
for(int x : List[c]) bit.update(x, -1);
int n = SZ(List[c]);
Ls[0] = 0;
for(int i=0; i<n; i++) {
int x = List[c][i];
Ls[i+1] = Ls[i] + bit.getSum(1, x);
}
Rs[n] = 0;
for(int i=n-1; i>=0; i--) {
int x = List[c][i];
Rs[i] = Rs[i+1] + bit.getSum(x, N);
}
ll now = LINF;
for(int i=0; i<=n; i++)
now = min(now, Ls[i] + Rs[i]);
ans += now;
}
printf("%lld\n", ans);
return 0;
}
Compilation message
growing.cpp: In function 'int main()':
growing.cpp:41: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]);
^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
15968 KB |
Output is correct |
2 |
Correct |
3 ms |
15968 KB |
Output is correct |
3 |
Correct |
0 ms |
15964 KB |
Output is correct |
4 |
Correct |
0 ms |
15968 KB |
Output is correct |
5 |
Correct |
0 ms |
15968 KB |
Output is correct |
6 |
Correct |
6 ms |
15968 KB |
Output is correct |
7 |
Correct |
3 ms |
15964 KB |
Output is correct |
8 |
Correct |
0 ms |
15960 KB |
Output is correct |
9 |
Correct |
3 ms |
15964 KB |
Output is correct |
10 |
Correct |
3 ms |
15968 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
15964 KB |
Output is correct |
2 |
Correct |
0 ms |
15964 KB |
Output is correct |
3 |
Correct |
0 ms |
15968 KB |
Output is correct |
4 |
Correct |
0 ms |
15964 KB |
Output is correct |
5 |
Correct |
0 ms |
15964 KB |
Output is correct |
6 |
Correct |
3 ms |
15960 KB |
Output is correct |
7 |
Correct |
6 ms |
15964 KB |
Output is correct |
8 |
Correct |
0 ms |
15968 KB |
Output is correct |
9 |
Correct |
6 ms |
15968 KB |
Output is correct |
10 |
Correct |
6 ms |
15968 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
15964 KB |
Output is correct |
2 |
Correct |
0 ms |
16100 KB |
Output is correct |
3 |
Correct |
3 ms |
16096 KB |
Output is correct |
4 |
Correct |
3 ms |
16096 KB |
Output is correct |
5 |
Correct |
6 ms |
16108 KB |
Output is correct |
6 |
Correct |
0 ms |
16104 KB |
Output is correct |
7 |
Correct |
3 ms |
16104 KB |
Output is correct |
8 |
Correct |
6 ms |
16104 KB |
Output is correct |
9 |
Correct |
6 ms |
16108 KB |
Output is correct |
10 |
Correct |
3 ms |
16108 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
29 ms |
16696 KB |
Output is correct |
2 |
Correct |
83 ms |
19596 KB |
Output is correct |
3 |
Correct |
133 ms |
21180 KB |
Output is correct |
4 |
Correct |
179 ms |
22576 KB |
Output is correct |
5 |
Correct |
83 ms |
23272 KB |
Output is correct |
6 |
Correct |
59 ms |
19596 KB |
Output is correct |
7 |
Correct |
99 ms |
20204 KB |
Output is correct |
8 |
Correct |
179 ms |
27464 KB |
Output is correct |
9 |
Correct |
196 ms |
23704 KB |
Output is correct |
10 |
Correct |
226 ms |
27464 KB |
Output is correct |