# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
163192 | TadijaSebez | 즐거운 채소 기르기 (JOI14_growing) | C++11 | 148 ms | 9316 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>
using namespace std;
#define pb push_back
const int N=300050;
int a[N],l[N],r[N];
int main()
{
int n;
scanf("%i",&n);
vector<int> all;
for(int i=1;i<=n;i++) scanf("%i",&a[i]),all.pb(a[i]);
sort(all.begin(),all.end());
all.resize(unique(all.begin(),all.end())-all.begin());
vector<int> sum(all.size()+1,0);
auto Set=[&](int i, int f){ for(;i<=all.size();i+=i&-i) sum[i]+=f;};
auto Get=[&](int i){ int ans=0;for(;i;i-=i&-i) ans+=sum[i];return ans;};
for(int i=1;i<=n;i++) a[i]=lower_bound(all.begin(),all.end(),a[i])-all.begin()+1;
for(int i=1;i<=n;i++) l[i]=Get(all.size())-Get(a[i]),Set(a[i],1);
sum.clear();sum.resize(all.size()+1);
for(int i=n;i>=1;i--) r[i]=Get(all.size())-Get(a[i]),Set(a[i],1);
long long ans=0;
for(int i=1;i<=n;i++) ans+=min(l[i],r[i]);
printf("%lld\n",ans);
return 0;
}
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... |