# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
25745 | dotorya | 즐거운 채소 기르기 (JOI14_growing) | C++14 | 189 ms | 28484 KiB |
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 <stdio.h>
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cmath>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <time.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#pragma warning(disable:4996)
#pragma comment(linker, "/STACK:336777216")
using namespace std;
#define mp make_pair
#define Fi first
#define Se second
#define pb(x) push_back(x)
#define szz(x) ((int)(x).size())
#define rep(i, n) for(int i=0;i<n;i++)
#define all(x) (x).begin(), (x).end()
#define ldb ldouble
typedef tuple<int, int, int> t3;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ldb;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair <ll, int> pli;
typedef pair <db, db> pdd;
int IT_MAX = 1 << 19;
const ll MOD = 1000000007;
const int INF = 0x3f3f3f3f;
const ll LL_INF = 0x3f3f3f3f3f3f3f3f;
const db PI = acos(-1);
const db ERR = 1e-10;
int bit[530000];
void update(int p, int v) {
for (; p <= IT_MAX; p += p & (-p)) bit[p] += v;
}
int getsum(int p) {
int rv = 0;
for (; p; p -= p & (-p)) rv += bit[p];
return rv;
}
int in[300050];
vector <int> Vp[300050];
vector <int> Vx;
ll u[300050];
ll sum[300050];
int main() {
int N, i, j;
scanf("%d", &N);
for (i = 1; i <= N; i++) {
scanf("%d", &in[i]);
Vx.push_back(in[i]);
}
sort(all(Vx));
Vx.erase(unique(all(Vx)), Vx.end());
for (i = 1; i <= N; i++) {
int p = lower_bound(all(Vx), in[i]) - Vx.begin();
Vp[p].push_back(i);
}
ll ans = 0;
for (i = 1; i <= N; i++) update(i, 1);
for (i = 0; i < Vx.size(); i++) {
int X = Vp[i].size();
vector <int> Vu;
for (j = 0; j < X; j++) u[j + 1] = getsum(Vp[i][j]);
int N2 = getsum(N);
for (j = 1; j <= X; j++) sum[j] = sum[j - 1] + u[j];
ll mn = LL_INF;
for (j = 0; j <= X; j++) {
ll v = sum[j] - (ll)j * (j + 1) / 2;
v += (ll)(X - j) * (2 * N2 - X + j + 1) / 2;
v -= sum[X] - sum[j];
mn = min(mn, v);
}
ans += mn;
for (auto it : Vp[i]) update(it, -1);
}
return !printf("%lld\n", ans);
}
Compilation message (stderr)
# | 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... |