結果
| 問題 |
No.837 Noelちゃんと星々2
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2019-09-08 14:41:15 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 21 ms / 2,000 ms |
| コード長 | 1,385 bytes |
| コンパイル時間 | 1,421 ms |
| コンパイル使用メモリ | 172,088 KB |
| 実行使用メモリ | 5,376 KB |
| 最終ジャッジ日時 | 2024-06-11 14:26:55 |
| 合計ジャッジ時間 | 2,598 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 29 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
long long diffsum(const vector<long long> & Ys, int head, int tail){
long long result = 0LL;
while (head < tail){
result += Ys[tail--] - Ys[head++];
}
return result;
}
long long solve(int N, const vector<long long> & Ys){
int a_head = 0;
int a_tail = 0;
int b_head = 1;
int b_tail = N - 1;
int a_mid = (a_head + a_tail) / 2;
int b_mid = (b_head + b_tail) / 2;
long long score = diffsum(Ys, a_head, a_tail) + diffsum(Ys, b_head, b_tail);
long long record = score;
for (a_tail = 1, b_head = 2; b_head < N; b_head++, a_tail++){
a_mid = (a_head + a_tail) / 2;
score += Ys[a_tail] - Ys[a_mid];
b_mid = (b_head + b_tail) / 2;
score -= Ys[b_mid] - Ys[b_head - 1];
record = min(record, score);
}
return record;
}
int main(){
cin.tie(0);
ios::sync_with_stdio(false);
int N;
cin >> N;
vector<long long> Ys(N, 0LL);
for (auto & y: Ys) cin >> y;
sort(Ys.begin(), Ys.end());
// Corner case
if (Ys[0] == Ys[N - 1]){
cout << 1 << endl;
return 0;
}
if (N == 2){
cout << 0 << endl;
return 0;
}else if (N == 3){
cout << min(Ys[2] - Ys[1], Ys[1] - Ys[0]) << endl;
return 0;
}
// Regular case
cout << solve(N, Ys) << endl;
return 0;
}