# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
25778 | khsoo01 | 즐거운 채소 기르기 (JOI14_growing) | C++11 | 233 ms | 30204 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<bits/stdc++.h>
#define X first
#define Y second
using namespace std;
typedef long long ll;
typedef pair<ll,ll> pll;
const ll N = 300005, inf = 1e18;
ll n, a[N], ans;
vector<pll> v;
struct segtree {
ll val[4*N], lim;
void init () {
for(lim=1;lim<=n;lim<<=1);
for(ll i=1;i<=n;i++) val[lim+i] = 1;
for(ll i=lim;--i;) val[i] = val[2*i] + val[2*i+1];
}
void upd (ll P, ll V) {
P += lim; val[P] = V; P >>= 1;
while(P) {val[P] = val[2*P] + val[2*P+1]; P >>= 1;}
}
ll get (ll S, ll E) {
ll R = 0; S += lim; E += lim;
while(S<=E) {
if(S%2==1) R += val[S++];
if(E%2==0) R += val[E--];
S >>= 1; E >>= 1;
}
return R;
}
} seg;
int main()
{
scanf("%lld",&n);
for(ll i=1;i<=n;i++) {
scanf("%lld",&a[i]);
v.push_back({a[i], i});
}
sort(v.begin(), v.end());
seg.init();
for(ll i=0;i<v.size();) {
vector<ll> I, L, R;
do {
I.push_back(v[i].Y);
seg.upd(v[i].Y, 0);
i++;
} while(i<v.size() && v[i].X == v[i-1].X);
L.push_back(0);
for(ll j=0;j<I.size();j++) {
L.push_back(L.back() + seg.get(1, I[j]));
}
R.push_back(0);
for(ll j=I.size();j--;) {
R.push_back(R.back() + seg.get(I[j], n));
}
ll C = inf;
for(ll i=0;i<=I.size();i++) {
C = min(C, L[i] + R[I.size()-i]);
}
ans += C;
}
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... |