結果

問題 No.3051 Make All Divisible
ユーザー Kude
提出日時 2025-03-07 21:47:34
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 1,950 bytes
コンパイル時間 4,079 ms
コンパイル使用メモリ 302,156 KB
実行使用メモリ 8,608 KB
最終ジャッジ日時 2025-03-07 21:47:40
合計ジャッジ時間 4,783 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 29 WA * 2
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
namespace {
#pragma GCC diagnostic ignored "-Wunused-function"
#include<atcoder/all>
#pragma GCC diagnostic warning "-Wunused-function"
using namespace std;
using namespace atcoder;
#define rep(i,n) for(int i = 0; i < (int)(n); i++)
#define rrep(i,n) for(int i = (int)(n) - 1; i >= 0; i--)
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
template<class T> bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; } else return false; }
template<class T> bool chmin(T& a, const T& b) { if (b < a) { a = b; return true; } else return false; }
using ll = long long;
using P = pair<int,int>;
using VI = vector<int>;
using VVI = vector<VI>;
using VL = vector<ll>;
using VVL = vector<VL>;

template <class T>
long long max_distinct_k(vector<T> a, int k) {
  const int n = a.size();
  if (n < k) return 0;
  sort(a.begin(), a.end());
  const int base = n - k;
  long long rest = accumulate(a.begin(), a.begin() + base, 0LL);
  for (int i = base + 1; i < n; i++) {
    long long mx = rest / (i - base);
    if (mx < a[i] - a[i - 1]) {
      return a[i - 1] + mx;
    }
    rest -= (a[i] - a[i - 1]) * (i - base);
  }
  return a[n - 1] + rest / k;
}

} int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int tt;
  cin >> tt;
  while (tt--) {
    int n, k;
    cin >> n >> k;
    VL r(n), a(n);
    rep(i, n) {
      cin >> a[i];
      r[i] = a[i] % k;
      a[i] -= r[i];
    }
    vector<int> ord(n);
    iota(ord.begin(), ord.end(), 0);
    sort(ord.begin(), ord.end(), [&](int i, int j) {
      return r[i] < r[j];
    });
    ll sm = accumulate(all(r), 0LL), mx = r[ord[n-1]];
    ll ans = [&]() {
      rep(_, n + 3) {
        for (int i : ord) {
          if (mx <= sm / k) return sm / k;
          if (a[i] >= k) {
            a[i] -= k;
            r[i] += k;
            sm += k;
            mx = r[i];
          }
        }
      }
      return -1LL;
    }();
    cout << ans << '\n';
  }
}
0