Submission #207716

#TimeUsernameProblemLanguageResultExecution timeMemory
207716lagoon전봇대 (KOI13_pole)C++14
32 / 100
30 ms760 KiB
// boj 8986 전봇대 #include <iostream> #include <algorithm> using namespace std; int position[100000], n; long long dist(int 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]; } int left = position[0]; int right = position[n - 1]; while (right - left > 3) { long long mid1 = left + (right - left) / 3; long long mid2 = right - (right - left) / 3; long long ret1 = dist(mid1); long long ret2 = dist(mid2); if (ret1 < ret2) right = mid2; else if (ret1 > ret2) left = mid1; else { left = mid1; right = mid2; } } long long ans = dist(right); for (int i = left; i < right; ++i) ans = min(ans, dist(i)); cout << ans; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...