結果

問題 No.3363 Two Closest Numbers
コンテスト
ユーザー HoyHoyCharhang
提出日時 2025-11-17 21:47:38
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 50 ms / 2,000 ms
コード長 3,581 bytes
コンパイル時間 3,433 ms
コンパイル使用メモリ 286,848 KB
実行使用メモリ 7,720 KB
最終ジャッジ日時 2025-11-17 21:47:50
合計ジャッジ時間 6,974 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 59
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define fi first
#define se second
#define rep(i,s,n) for (int i = (s); i < (n); ++i)
#define rrep(i,n,g) for (int i = (n)-1; i >= (g); --i)
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define len(x) (int)(x).size()
#define dup(x,y) (((x)+(y)-1)/(y))
#define pb push_back
#define eb emplace_back
#define Field(T) vector<vector<T>>
using namespace std;
using ll = long long;
using ull = unsigned long long;
template<typename T> using pq = priority_queue<T,vector<T>,greater<T>>;
using P = pair<int,int>;
template<class T>bool chmax(T&a,T b){if(a<b){a=b;return 1;}return 0;}
template<class T>bool chmin(T&a,T b){if(b<a){a=b;return 1;}return 0;}

template< int mod >
struct ModInt {
  int x;
  ModInt() : x(0) {}
  ModInt(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}
  ModInt &operator+=(const ModInt &p) {
    if((x += p.x) >= mod) x -= mod;
    return *this;
  }
  ModInt &operator-=(const ModInt &p) {
    if((x += mod - p.x) >= mod) x -= mod;
    return *this;
  }
  ModInt &operator*=(const ModInt &p) {
    x = (int) (1LL * x * p.x % mod);
    return *this;
  }
  ModInt &operator/=(const ModInt &p) {
    *this *= p.inverse();
    return *this;
  }
  ModInt operator-() const { return ModInt(-x); }
  ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; }
  ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; }
  ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; }
  ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; }
  bool operator==(const ModInt &p) const { return x == p.x; }
  bool operator!=(const ModInt &p) const { return x != p.x; }

  ModInt inverse() const {
    assert(x);
    int a = x, b = mod, u = 1, v = 0, t;
    while(b > 0) {
      t = a / b;
      swap(a -= t * b, b);
      swap(u -= t * v, v);
    }
    return ModInt(u);
  }

  ModInt pow(int64_t n) const {
    ModInt ret(1), mul(x);
    while(n > 0) {
      if(n & 1) ret *= mul;
      mul *= mul;
      n >>= 1;
    }
    return ret;
  }

  friend ostream &operator<<(ostream &os, const ModInt &p) {
    return os << p.x;
  }

  friend istream &operator>>(istream &is, ModInt &a) {
    int64_t t;
    is >> t;
    a = ModInt< mod >(t);
    return (is);
  }

  static int get_mod() { return mod; }
};

using mint = ModInt<998244353>;

int main() {
  int n;
  cin >> n;
  vector<int> c(n);
  rep(i,0,n) cin >> c[i];
  if (n % 2 == 1) {
    sort(all(c));
    mint v1 = c[0], v2 = 0;
    rep(i,0,n/2) {
      v1 = v1*10 + c[i+1];
      v2 = v2*10 + c[n-i-1];
    }
    // cout << v1 << " " << v2 << endl;
    cout << v1-v2 << endl;
  } else {
    vector<int> cnt(10, 0);
    rep(i,0,n) ++cnt[c[i]];
    vector<int> x;
    rep(i,1,10) {
      if (cnt[i] % 2 == 1) x.eb(i);
    }
    // for (int e : x) {
    //   cout << e << " ";
    // }
    // cout << endl;
    function<int(vector<int>)> all_search = [](vector<int> v) {
      int ret = 1000000000;
      sort(all(v));
      int l = len(v);
      do {
        int v1 = 0, v2 = 0;
        rep(i,0,l/2) {
          v1 = v1*10 + v[i];
          v2 = v2*10 + v[i+l/2];
        }
        ret = min(ret, abs(v1-v2));
      } while(next_permutation(all(v)));
      return ret;
    };
    int l = len(x);
    int ans = all_search(x);
    // cout << ans << endl;
    if (l <= 4) {
      rep(i,1,10) {
        if (cnt[i] % 2 == 0 && cnt[i] >= 2) {
          x.eb(i), x.eb(i);
          ans = min(ans, all_search(x));
          x.pop_back(), x.pop_back();
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}
0