#include <bits/stdc++.h>
using namespace std;
#define long long long
#define pii pair<long, long>
#define x first
#define y second
const int N = 1e5+5;
int n;
int A[N], pref[N], posf[N];
vector<int> coor1, coor2;
pii t1[N], t2[N];
int get(vector<int> &v, int x) {
return upper_bound(v.begin(), v.end(), x) - v.begin();
}
void add(pii &a, pii b) { a = pii(a.x + b.x, a.y + b.y); }
void update(pii t[], int x, pii v) {
for(; x <= n; x += x&-x) add(t[x], v);
}
pii query(pii t[], int x) {
pii ret;
for(; x; x -= x&-x) add(ret, t[x]);
return ret;
}
long getVal(int m, int i) {
long sum = 0;
pii rhs1 = query(t2, get(coor2, m+i));
pii rhs2 = query(t2, n);
sum += rhs1.y * (m+i) - rhs1.x + (rhs2.x - rhs1.x) - (rhs2.y - rhs1.y) * (m+i);
i = n-i+1;
pii lhs1 = query(t1, get(coor1, m+i));
pii lhs2 = query(t1, n);
sum += lhs1.y * (m+i) - lhs1.x + (lhs2.x - lhs1.x) - (lhs2.y - lhs1.y) * (m+i);
return sum;
}
int main() {
scanf("%d", &n);
for(int i = 1; i <= n; ++i) scanf("%d", A+i);
for(int i = 1; i <= n; ++i) pref[i] = A[i]+n-i+1, posf[i] = A[i]+i;
for(int i = 1; i <= n; ++i) coor1.emplace_back(pref[i]), coor2.emplace_back(posf[i]);
sort(coor1.begin(), coor1.end()), sort(coor2.begin(), coor2.end());
coor1.resize(unique(coor1.begin(), coor1.end()) - coor1.begin());
coor2.resize(unique(coor2.begin(), coor2.end()) - coor2.begin());
for(int i = 1; i <= n; ++i) update(t2, get(coor2, posf[i]), pii(posf[i], 1));
long mn = 1e18;
for(int i = 1; i <= n; ++i) {
int l = max(i, n-i+1), r = 1e9;
while(l < r) {
int m = l + r >> 1;
if(getVal(m+1, i) > getVal(m, i)) r = m;
else l = m+1;
}
mn = min(mn, getVal(r, i));
if(getVal(r, i) == 2) printf("%d %d\n", r, i);
update(t1, get(coor1, pref[i]), pii(pref[i], 1));
update(t2, get(coor2, posf[i]), pii(-posf[i], -1));
}
printf("%lld\n", mn);
}
Compilation message
krov.cpp: In function 'int main()':
krov.cpp:57:23: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int m = l + r >> 1;
~~^~~
krov.cpp:45:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &n);
~~~~~^~~~~~~~~~
krov.cpp:46:38: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
for(int i = 1; i <= n; ++i) scanf("%d", A+i);
~~~~~^~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
504 KB |
Output is correct |
2 |
Correct |
9 ms |
600 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
600 KB |
Output is correct |
2 |
Correct |
8 ms |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
740 KB |
Output is correct |
2 |
Correct |
13 ms |
740 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
816 KB |
Output is correct |
2 |
Correct |
17 ms |
840 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
1068 KB |
Output is correct |
2 |
Correct |
20 ms |
1068 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
31 ms |
1196 KB |
Output is correct |
2 |
Correct |
30 ms |
1280 KB |
Output is correct |
3 |
Correct |
17 ms |
1280 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
204 ms |
2540 KB |
Output is correct |
2 |
Correct |
217 ms |
2540 KB |
Output is correct |
3 |
Correct |
231 ms |
3000 KB |
Output is correct |
4 |
Correct |
278 ms |
3344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
415 ms |
4124 KB |
Output is correct |
2 |
Correct |
350 ms |
4732 KB |
Output is correct |
3 |
Correct |
300 ms |
4876 KB |
Output is correct |
4 |
Correct |
269 ms |
5132 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
641 ms |
6348 KB |
Output is correct |
2 |
Correct |
565 ms |
8604 KB |
Output is correct |
3 |
Correct |
376 ms |
8604 KB |
Output is correct |
4 |
Correct |
776 ms |
9024 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
827 ms |
11344 KB |
Output is correct |
2 |
Correct |
785 ms |
12296 KB |
Output is correct |
3 |
Correct |
736 ms |
12296 KB |
Output is correct |
4 |
Correct |
1010 ms |
13088 KB |
Output is correct |
5 |
Correct |
240 ms |
13088 KB |
Output is correct |