This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 (stderr)
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]);
^
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |