結果
| 問題 |
No.837 Noelちゃんと星々2
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2019-09-08 14:31:41 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 19 ms / 2,000 ms |
| コード長 | 1,562 bytes |
| コンパイル時間 | 2,125 ms |
| コンパイル使用メモリ | 171,668 KB |
| 実行使用メモリ | 5,376 KB |
| 最終ジャッジ日時 | 2024-06-11 14:26:52 |
| 合計ジャッジ時間 | 2,689 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| 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(long long record, int N, const vector<long long> & Ys){
int a_head = 0;
int a_tail = 1;
int b_head = 2;
int b_tail = N - 1;
int a_mid = (a_head + a_tail) / 2;
int b_mid = (b_head + b_tail) / 2;
long long sum_a = diffsum(Ys, a_head, a_tail);
long long sum_b = diffsum(Ys, b_head, b_tail);
record = min(record, sum_a + sum_b);
for (a_tail = 2, b_head = 3; b_head < N - 1; b_head++, a_tail++){
a_mid = (a_head + a_tail) / 2;
sum_a += Ys[a_tail] - Ys[a_mid];
b_mid = (b_head + b_tail) / 2;
sum_b -= Ys[b_mid] - Ys[b_head - 1];
record = min(record, sum_a + sum_b);
}
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;
}
long long record;
record = diffsum(Ys, 0, N - 2);
record = min(record, diffsum(Ys, 1, N - 1));
// Regular case
cout << solve(record, N, Ys) << endl;
return 0;
}