結果
| 問題 |
No.3363 Two Closest Numbers
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2025-11-17 21:46:02 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 25 ms / 2,000 ms |
| コード長 | 2,095 bytes |
| コンパイル時間 | 4,095 ms |
| コンパイル使用メモリ | 258,120 KB |
| 実行使用メモリ | 7,844 KB |
| 最終ジャッジ日時 | 2025-11-17 21:46:32 |
| 合計ジャッジ時間 | 6,971 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 59 |
ソースコード
#include <bits/stdc++.h>
#include <atcoder/all>
using namespace std;
using isize = size_t;
using i32 = int;
using u32 = unsigned int;
using i64 = long long;
using u64 = unsigned long long;
using i128 = __int128_t;
using u128 = __uint128_t;
using f64 = long double;
using p2 = pair<i64, i64>;
using el = tuple<i64, i64, i64>;
using mint = atcoder::modint998244353;
void _main();
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
cout << fixed << setprecision(18);
_main();
}
void _main() {
i64 n;
cin >> n;
vector<i64> c(n);
for (i64 i = 0; i < n; i++) {
cin >> c[i];
}
if (n % 2 == 1) {
sort(c.begin(), c.end());
i64 a = (n + 1) / 2;
mint x = 0, y = 0;
for (i64 i = 0; i < a; i++) {
x = x * 10 + c[i];
}
for (i64 i = n - 1; i >= a; i--) {
y = y * 10 + c[i];
}
cout << (x - y).val() << "\n";
return;
}
vector<i64> cnt(10, 0);
for (i64 c : c) cnt[c]++;
vector<i64> d;
for (i64 i = 0; i < 10; i++) if (cnt[i] % 2 != 0) d.push_back(i);
if (d.size() == 0) {
cout << "0\n";
return;
}
i64 ans = 1e18;
auto solve = [](vector<i64> d) {
sort(d.begin(), d.end());
i64 ans = 1e18;
for (i64 i = 0; i < d.size(); i++) {
for (i64 j = i + 1; j < d.size(); j++) {
for (i64 xx = 0; xx < 1ll << d.size(); xx++) {
i64 x = xx;
i64 y = (1ll << d.size()) - x - 1;
x &= (1ll << d.size()) - 1 - (1ll << i) - (1ll << j);
y &= (1ll << d.size()) - 1 - (1ll << i) - (1ll << j);
if (__builtin_popcountll(x) != __builtin_popcountll(y)) continue;
i64 u = d[i], v = d[j];
for (i64 k = d.size() - 1; k >= 0; k--) if (x >> k & 1) u = u * 10 + d[k];
for (i64 k = 0; k < d.size(); k++) if (y >> k & 1) v = v * 10 + d[k];
ans = min(ans, abs(v - u));
}
}
}
return ans;
};
ans = min(ans, solve(d));
for (i64 i = 0; i < 10; i++) if (cnt[i] >= 2) {
d.push_back(i), d.push_back(i);
ans = min(ans, solve(d));
d.pop_back(), d.pop_back();
}
cout << mint(ans).val() << "\n";
}