# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
81611 |
2018-10-25T15:09:30 Z |
luckyboy |
Krov (COCI17_krov) |
C++14 |
|
396 ms |
5824 KB |
/**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) - 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));
}
write(ans);
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
540 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
540 KB |
Output is correct |
2 |
Correct |
5 ms |
540 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
628 KB |
Output is correct |
2 |
Correct |
6 ms |
628 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
720 KB |
Output is correct |
2 |
Correct |
8 ms |
840 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
840 KB |
Output is correct |
2 |
Correct |
9 ms |
840 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
892 KB |
Output is correct |
2 |
Correct |
13 ms |
892 KB |
Output is correct |
3 |
Correct |
9 ms |
892 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
99 ms |
1784 KB |
Output is correct |
2 |
Correct |
87 ms |
1784 KB |
Output is correct |
3 |
Correct |
93 ms |
1784 KB |
Output is correct |
4 |
Correct |
117 ms |
1912 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
169 ms |
2420 KB |
Output is correct |
2 |
Correct |
175 ms |
2680 KB |
Output is correct |
3 |
Correct |
121 ms |
2680 KB |
Output is correct |
4 |
Correct |
112 ms |
2680 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
265 ms |
2688 KB |
Output is correct |
2 |
Correct |
223 ms |
3948 KB |
Output is correct |
3 |
Correct |
141 ms |
3948 KB |
Output is correct |
4 |
Correct |
320 ms |
4500 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
332 ms |
5824 KB |
Output is correct |
2 |
Correct |
326 ms |
5824 KB |
Output is correct |
3 |
Correct |
305 ms |
5824 KB |
Output is correct |
4 |
Correct |
396 ms |
5824 KB |
Output is correct |
5 |
Correct |
75 ms |
5824 KB |
Output is correct |