This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/**Lucky Boy**/
#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define FORD(i, a, b) for (int i = (a); i >= (b); --i)
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define maxc 1000000007
#define maxn 100005
#define maxm 500005
#define pii pair <int,int>
#define Task ""
template <typename T> inline void read(T &x){char c;bool nega=0;while((!isdigit(c=getchar()))&&(c!='-'));if(c=='-'){nega=1;c=getchar();}x=c-48;while(isdigit(c=getchar())) x=x*10+c-48;if(nega) x=-x;}
template <typename T> inline void writep(T x){if(x>9) writep(x/10);putchar(x%10+48);}
template <typename T> inline void write(T x){if(x<0){putchar('-');x=-x;}writep(x);putchar(' ');}
template <typename T> inline void writeln(T x){write(x);putchar('\n');}
using namespace std;
int n,a[maxn];
long long ans = 1ll*maxc*maxc;
struct BIT
{
long long t[maxn];
void Update(int x,int val)
{
while (x <= n)
{
t[x] += val;
x += x & -x;
}
}
long long Get(int x)
{
long long res = 0;
while (x)
{
res += t[x];
x -= x & -x;
}
return res;
}
}l1,l2,r1,r2;
vector <int> L,R;
bool check(int i,int x)
{
int pos1 = upper_bound(L.begin(),L.end(),x - i) - L.begin(),
pos2 = upper_bound(R.begin(),R.end(),x + i) - R.begin();
pos1 = l1.Get(pos1);
pos2 = r1.Get(pos2);
return (pos1 + pos2) > (n - n / 2);
}
long long Calc(int i,int x)
{
long long res = 0;
int il = upper_bound(L.begin(), L.end(), x - i) - L.begin();
int ir = upper_bound(R.begin(), R.end(), x + i) - R.begin();
res += 1ll*(x - i) * l1.Get(il) - l2.Get(il);
res += (l2.Get(n) - l2.Get(il)) - (1ll*(x - i) * (l1.Get(n) - l1.Get(il)));
res += 1ll*(x + i) * r1.Get(ir) - r2.Get(ir);
res += (r2.Get(n) - r2.Get(ir)) - (1ll*(x + i) * (r1.Get(n) - r1.Get(ir)));
return res;
}
int main()
{
//ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
//freopen(Task".inp", "r",stdin);
//freopen(Task".out", "w",stdout);
read(n);
FOR(i,1,n)
{
read(a[i]);
L.pb(a[i] - i);
R.pb(a[i] + i);
}
sort(L.begin(),L.end());
sort(R.begin(),R.end());
L.resize(unique(L.begin(),L.end()) - L.begin());
R.resize(unique(R.begin(),R.end()) - R.begin());
FOR(i,1,n)
{
int pos = upper_bound(R.begin(),R.end(),a[i] + i) - R.begin();
r1.Update(pos,1);
r2.Update(pos,a[i] + i);
}
FOR(i,1,n)
{
int pos1 = upper_bound(L.begin(),L.end(),a[i] - i) - L.begin(),
pos2 = upper_bound(R.begin(),R.end(),a[i] + i) - R.begin();
l1.Update(pos1,1);
l2.Update(pos1,a[i] - i);
r1.Update(pos2,-1);
r2.Update(pos2,-a[i] - i);
int l = max(i,n-i+1),r = maxc + maxn * 2;
while (r - l > 1)
{
int mid = (l+r) >> 1;
if (check(i,mid)) r = mid;
else l = mid;
}
ans = min(ans,Calc(i,r));
ans = min(ans,Calc(i,l));
}
write(ans);
return 0;
}
# | 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... |
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |