結果
| 問題 |
No.3363 Two Closest Numbers
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2025-10-29 13:51:05 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 16 ms / 2,000 ms |
| コード長 | 1,997 bytes |
| コンパイル時間 | 3,957 ms |
| コンパイル使用メモリ | 258,240 KB |
| 実行使用メモリ | 7,716 KB |
| 最終ジャッジ日時 | 2025-11-17 20:34:30 |
| 合計ジャッジ時間 | 6,518 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 59 |
ソースコード
#include <bits/stdc++.h>
#include <atcoder/all>
using namespace std;
using ll = long long;
#define rep(i, s, t) for (ll i = s; i < (ll)(t); i++)
#define all(x) begin(x), end(x)
template <class T> bool chmin(T& x, T y) {
return x > y ? (x = y, true) : false;
}
template <class T> bool chmax(T& x, T y) {
return x < y ? (x = y, true) : false;
}
struct io_setup {
io_setup() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout << fixed << setprecision(15);
}
} io_setup;
using mint = atcoder::modint998244353;
vector<ll> pw10(10);
pair<ll, ll> build_min_max(vector<ll> v) {
int m = v.size();
assert(m % 2 == 0 && m <= 10);
ll x = 0, y = 0;
rep(i, 0, m / 2) x = x * 10 + v[i];
rep(i, m / 2, m) y += v[i] * pw10[i - m / 2];
return {x, y};
}
ll build_abs_min(vector<ll> v) {
sort(v.begin(), v.end());
int m = v.size();
assert(m % 2 == 0);
ll res = 1e18;
rep(i, 0, m - 1) {
auto w = v;
w.erase(w.begin() + i);
w.erase(w.begin() + i);
auto [x, y] = build_min_max(w);
x += pw10[m / 2 - 1] * v[i + 1];
y += pw10[m / 2 - 1] * v[i];
chmin(res, abs(x - y));
}
return res;
}
void solve() {
pw10[0] = 1;
rep(i, 1, 10) pw10[i] = pw10[i - 1] * 10;
int n;
cin >> n;
vector<int> c(10);
rep(i, 0, n) {
int t;
cin >> t;
c[t]++;
}
if (n & 1) {
mint x = 0, y = 0;
int szx = (n + 1) / 2, szy = n / 2;
for (int i = 1; i <= 9; i++) {
while (szx > 0 && c[i] > 0) {
szx--;
c[i]--;
x = x * 10 + i;
}
}
for (int i = 9; i >= 1; i--) {
while (szy > 0 && c[i] > 0) {
szy--;
c[i]--;
y = y * 10 + i;
}
}
cout << (x - y).val() << '\n';
} else {
vector<ll> odd;
rep(i, 0, 10) if (c[i] & 1) odd.push_back(i);
ll res = 1e18;
chmin(res, build_abs_min(odd));
rep(i, 1, 10) if (c[i] > 0 && c[i] % 2 == 0) {
odd.push_back(i);
odd.push_back(i);
chmin(res, build_abs_min(odd));
odd.pop_back();
odd.pop_back();
}
cout << res << '\n';
}
}
int main() {
int t = 1;
// cin >> t;
while (t--) solve();
}