// boj 8986 전봇대
#include <iostream>
#include <algorithm>
using namespace std;
int position[100000], n;
long long dist(long long x) {
long long ret = 0;
for (int i = 0; i < n; ++i) {
ret += abs(x * i - position[i]);
}
return ret;
}
int main(void) {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n;
for (int i = 0; i < n; ++i) {
cin >> position[i];
}
long long left = position[0];
long long right = position[n-1];
while (right - left > 3) {
long long mid1 = left + (right - left) / 3;
long long mid2 = right - (right - left) / 3;
if (dist(mid1) < dist(mid2)) right = mid2;
else left = mid1;
}
long long ans = dist(right);
for (int i = left; i < right; ++i)
ans = min(ans, dist(i));
cout << ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
4 ms |
376 KB |
Output is correct |
3 |
Correct |
5 ms |
376 KB |
Output is correct |
4 |
Correct |
4 ms |
252 KB |
Output is correct |
5 |
Correct |
5 ms |
376 KB |
Output is correct |
6 |
Correct |
4 ms |
376 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
5 ms |
376 KB |
Output is correct |
9 |
Correct |
5 ms |
376 KB |
Output is correct |
10 |
Correct |
5 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
376 KB |
Output is correct |
3 |
Correct |
5 ms |
376 KB |
Output is correct |
4 |
Correct |
5 ms |
376 KB |
Output is correct |
5 |
Correct |
5 ms |
376 KB |
Output is correct |
6 |
Correct |
5 ms |
376 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
5 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
376 KB |
Output is correct |
2 |
Correct |
6 ms |
504 KB |
Output is correct |
3 |
Correct |
8 ms |
504 KB |
Output is correct |
4 |
Correct |
7 ms |
504 KB |
Output is correct |
5 |
Correct |
7 ms |
504 KB |
Output is correct |
6 |
Correct |
7 ms |
504 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
30 ms |
760 KB |
Output is correct |
2 |
Correct |
29 ms |
1552 KB |
Output is correct |
3 |
Correct |
31 ms |
1656 KB |
Output is correct |
4 |
Correct |
30 ms |
1656 KB |
Output is correct |
5 |
Correct |
31 ms |
1716 KB |
Output is correct |
6 |
Correct |
30 ms |
1656 KB |
Output is correct |