You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
a[i]를 정렬한 다음 중앙값으로 b를 설정하고 정답을 구하면 된다.
위 중앙값을 골라야 한다는 사실을 모르면 쉽지 않은 문제.
Source Code
#include<iostream>
#include<algorithm>usingnamespacestd;int a[200000];
intmain() {
int n;
cin >> n;
for (int i = 0; i < n; i++)
{
cin >> a[i];
a[i] -= (i + 1);
}
sort(a, a + n);
int median = a[(n - 1) / 2];
longlong ans = 0;
for (int i = 0; i < n; i++)
{
ans += abs(a[i] - median);
}
cout << ans << endl;
}
This discussion was converted from issue #25 on September 15, 2026 10:45.
Heading
Bold
Italic
Quote
Code
Link
Numbered list
Unordered list
Task list
Attach files
Mention
Reference
Menu
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Problem link
https://atcoder.jp/contests/arc100/tasks/arc100_a
Problem Summary
N 개의 a 배열에서 적당한 b 값을 구해서 아래 식을 최소로 만드는 b 값을 찾는 문제.

Solution
먼저 자기 인덱스 + 1만큼 빼고 시작하자.
그러면 | a[i] - b | 의 최솟값을 찾는 문제가 되는데..
이건 중앙값을 쓰면 된다.
자세한 증명은 https://drken1215.hatenablog.com/entry/2019/06/15/114700 참고.
a[i]를 정렬한 다음 중앙값으로 b를 설정하고 정답을 구하면 된다.
위 중앙값을 골라야 한다는 사실을 모르면 쉽지 않은 문제.
Source Code
All reactions