Submission #81607

#TimeUsernameProblemLanguageResultExecution timeMemory
81607luckyboyKrov (COCI17_krov)C++14
98 / 140
414 ms7492 KiB
/**Lucky Boy**/ #include <bits/stdc++.h> #define FOR(i, a, b) for (long long i = (a); i <= (b); ++i) #define FORD(i, a, b) for (long long 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 <long long,long long> #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; long long n,a[maxn]; long long ans = 1ll*maxc*maxc; struct BIT { long long t[maxn]; void Update(long long x,long long val) { while (x <= n) { t[x] += val; x += x & -x; } } long long Get(long long x) { long long res = 0; while (x) { res += t[x]; x -= x & -x; } return res; } }l1,l2,r1,r2; vector <long long> L,R; bool check(long long i,long long x) { long long 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(long long i,long long x) { long long res = 0; long long il = upper_bound(L.begin(), L.end(), x - i) - L.begin(); long long 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) { long long 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) { long long 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); long long l = max(i,n-i+1),r = maxc + maxn * 2; while (r - l > 1) { long long 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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...