[AtCoder] ABC 132 C – Divide the Problems
問題
方針
配列 \( d \) の順序は入れ替えても大丈夫なので、昇順にソートさせます。\( N \) は偶数なので、\( d \) の中央値をそれぞれ、\( c_1 = d_{N/2 – 1} \)、\( c_2 = d_{N/2} \) とします。したがって、\( K \) の最小値は \( c_1 \) であり、最大値は \( c_2 \) となるので、求める答えは、\( c_2 – c_1 \) となります。
コード
#include <bits/stdc++.h> using namespace std; typedef long long ll; int main() { cin.tie(0); ios::sync_with_stdio(false); int N; cin >> N; vector<int> d(N); for (int i = 0; i < N; i++) { cin >> d[i]; } sort(d.begin(), d.end()); int l = d[N/2 - 1]; int r = d[N/2]; cout << (r - l) << "\n"; return 0; }
ディスカッション
コメント一覧
まだ、コメントがありません