結果
| 問題 |
No.3051 Make All Divisible
|
| コンテスト | |
| ユーザー |
Nachia
|
| 提出日時 | 2025-03-07 22:05:31 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 2,318 bytes |
| コンパイル時間 | 1,452 ms |
| コンパイル使用メモリ | 88,384 KB |
| 実行使用メモリ | 8,612 KB |
| 最終ジャッジ日時 | 2025-03-07 22:05:35 |
| 合計ジャッジ時間 | 2,749 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 3 WA * 28 |
ソースコード
#ifdef NACHIA
#define _GLIBCXX_DEBUG
#else
#define NDEBUG
#endif
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <queue>
using i64 = long long;
using u64 = unsigned long long;
#define rep(i,n) for(i64 i=0; i<i64(n); i++)
const i64 INF = 1001001001001001001;
template<typename A> void chmin(A& l, const A& r){ if(r < l) l = r; }
template<typename A> void chmax(A& l, const A& r){ if(l < r) l = r; }
using namespace std;
void testcase(){
i64 N, K; cin >> N >> K;
vector<i64> A(N);
rep(i,N) cin >> A[i];
i64 F = 0; rep(i,N) F += A[i] % K;
if(F%K != 0){ cout << "-1\n"; return; }
sort(A.begin(), A.end());
vector<int> I(N); rep(i,N) I[i] = i;
sort(I.begin(), I.end(), [&](int l, int r){ return A[l]%K < A[r]%K; });
rep(x,N){
//cout << "x = " << x << endl;
vector<i64> B, C(N-x);
rep(i,N) if(I[i] >= x) B.push_back(A[I[i]]);
rep(i,N-x) C[i] = B[i] % K;
i64 mx = INF;
rep(i,N-x) chmin(mx, B[i] / K * (N-x) - 1);
//cout << "B = "; for(auto b : B){ cout << b << " "; } cout << endl;
//cout << "C = "; for(auto b : C){ cout << b << " "; } cout << endl;
i64 sumpre = 0;
rep(i,x) sumpre += A[i];
rep(i,N-x) sumpre += C[i];
sumpre /= K;
vector<i64> Q(N);
rep(i,x) Q[i] = A[i];
auto mkQ = [&](i64 t){
i64 b = t % (N-x);
i64 c = t / (N-x);
rep(i,N-x){
Q[x+i] = C[b++] + c * K;
if(b >= N-x){ b = 0; c++; }
}
};
i64 ng = -1;
i64 ok = mx + 1;
while(ng + 1 < ok){
i64 t = ng + (ok - ng) / 2;
i64 i = t % (N-x);
i64 c = t / (N-x);
i64 sum = 0;
bool judge = true;
mkQ(t);
rep(f,N){
sum += Q[f];
if(f > N-K && sum < (f-N+K) * sumpre) judge = false;
}
(judge ? ok : ng) = t;
}
if(ok <= mx){
i64 ans = sumpre + ok;
cout << ans << "\n";
return;
}
}
cout << "-1\n";
}
int main(){
ios::sync_with_stdio(false); cin.tie(nullptr);
i64 T; cin >> T;
rep(t,T)
testcase();
return 0;
}
Nachia