結果

問題 No.3363 Two Closest Numbers
コンテスト
ユーザー The Forsaking
提出日時 2025-11-23 15:05:08
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 1,217 bytes
コンパイル時間 2,318 ms
コンパイル使用メモリ 201,564 KB
実行使用メモリ 8,012 KB
最終ジャッジ日時 2025-11-23 15:05:15
合計ジャッジ時間 5,995 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 53 WA * 6
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:15:42: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   15 |     for (int i = 1; i < n + 1; i++) scanf("%d", w + i);
      |                                     ~~~~~^~~~~~~~~~~~~

ソースコード

diff #
raw source code

#include <bits/stdc++.h>

using namespace std;

typedef pair<int, int> pii;
typedef long long ll;
const int N = 2000010, MOD = 998244353, INF = 0x3f3f3f3f;
int n, m, w[N];


ll qmi(ll a, ll b, ll c) { ll res = 1; while (b) { if (b & 1) res = res * a % c; a = a * a % c; b >>= 1; } return res; }
int c[N];
int main() {
    cin >> n;
    for (int i = 1; i < n + 1; i++) scanf("%d", w + i);
    ll res = 0;
    sort(w + 1, w + n + 1);
    if (n & 1) {
        res = w[1] * qmi(10, n >> 1, MOD) % MOD;
        for (int l = 2, r = n; l < r; l++, r--) res = (res - (w[r] - w[l]) * qmi(10, (r - l) >> 1, MOD) % MOD + MOD) % MOD;
    } else {
        for (int i = 1; i < n + 1; i++) c[w[i]] ^= 1;
        vector<int> v;
        for (int i = 1; i < 10; i++)
            if (c[i])
                v.push_back(i);
        sort(v.begin(), v.end());
        res = 1e18;
        do {
            ll sum = 0;
            int c = v.size() >> 1;
            for (int i = 0; i < v.size(); i += 2) {
                sum = sum + (i ? -1 : 1) * abs(v[i + 1] - v[i]) * qmi(10, --c, MOD);
            }
            res = min(res, sum);
        } while (next_permutation(v.begin(), v.end()));
    }
    printf("%lld\n", res);
    return 0;
}
0