結果

問題 No.2860 Heal Slimes
ユーザー dyktr_06
提出日時 2025-04-18 16:08:01
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 4,026 bytes
コンパイル時間 2,088 ms
コンパイル使用メモリ 195,812 KB
実行使用メモリ 9,472 KB
最終ジャッジ日時 2025-04-18 16:08:07
合計ジャッジ時間 5,337 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 39 WA * 21
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

// o4-mini-high
int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    
    int T; cin >> T;
    while(T--){
	    int N, K;
	    ll X;
	    cin >> N >> K >> X;
	    vector<ll> H(N);
	    for (int i = 0; i < N; i++) {
	        cin >> H[i];
	    }
	
	    // If K == N, we can only add to all N slimes at once:
	    // differences remain the same, so only possible if all H are already equal.
	    if (K == N) {
	        bool all_eq = true;
	        for (int i = 1; i < N; i++) {
	            if (H[i] != H[0]) {
	                all_eq = false;
	                break;
	            }
	        }
	        cout << (all_eq ? "Yes\n" : "No\n");
	        return 0;
	    }
	
	    // Check the modulo invariant: all H[i] % X must be the same.
	    ll r = H[0] % X;
	    for (int i = 1; i < N; i++) {
	        if (H[i] % X != r) {
	            cout << "No\n";
	            return 0;
	        }
	    }
	
	    // Build a[i] = (H[i] - r) / X, so H[i] = X * a[i] + r.
	    vector<ll> a(N+1);
	    for (int i = 1; i <= N; i++) {
	        a[i] = (H[i-1] - r) / X;
	    }
	
	    // Build the "delta" array d[i] = a[i] - a[i-1] for i=2..N, and d[N+1] = -a[N].
	    vector<ll> d(N+2);
	    for (int i = 2; i <= N; i++) {
	        d[i] = a[i] - a[i-1];
	    }
	    d[N+1] = -a[N];
	
	    int M = N - K + 1;  // number of possible operation start positions
	
	    // Compute base_op[j] for j=1..M, assuming the final target a_f = 0.
	    // Later we'll adjust by a global C = a_f (the target a-value).
	    vector<ll> base_op(M+1);
	    base_op[1] = -a[1];
	    for (int j = 2; j <= M; j++) {
	        if (j <= K) {
	            // op_j = -d[j]
	            base_op[j] = -d[j];
	        } else {
	            // op_j = -d[j] + op_{j-K}
	            base_op[j] = -d[j] + base_op[j - K];
	        }
	    }
	
	    // For starts j where (j-1)%K != 0, base_op[j] must be >= 0 (no C adjustment).
	    for (int j = 1; j <= M; j++) {
	        if ((j - 1) % K != 0) {
	            if (base_op[j] < 0) {
	                cout << "No\n";
	                return 0;
	            }
	        }
	    }
	
	    // Compute the minimum C needed to make op_j >= 0 for j ≡ 1 (mod K):
	    // op_j = base_op[j] + C >= 0  =>  C >= -base_op[j]
	    ll C_min = LLONG_MIN;
	    for (int j = 1; j <= M; j++) {
	        if ((j - 1) % K == 0) {
	            C_min = max(C_min, -base_op[j]);
	        }
	    }
	
	    // Tail consistency constraints:
	    // For i = M+1..N, let j = i-K (so j in [N-2K+2 .. N-K]),
	    // we need d[i] = op_j.
	    int j_low = max(1, N - 2 * K + 2);
	    int j_high = N - K;
	
	    bool C_assigned = false;
	    ll C_val = 0;
	
	    for (int j = j_low; j <= j_high; j++) {
	        int i = j + K;  // the corresponding delta index
	        if ((j - 1) % K == 0) {
	            // op_j = base_op[j] + C must equal d[i]
	            ll neededC = d[i] - base_op[j];
	            if (!C_assigned) {
	                C_assigned = true;
	                C_val = neededC;
	            } else if (C_val != neededC) {
	                cout << "No\n";
	                return 0;
	            }
	        } else {
	            // op_j = base_op[j] must equal d[i]
	            if (base_op[j] != d[i]) {
	                cout << "No\n";
	                return 0;
	            }
	        }
	    }
	
	    // The final target a-value must be at least max(a[i]) so that final HP >= max(H).
	    ll q = 0;
	    for (int i = 1; i <= N; i++) {
	        q = max(q, a[i]);
	    }
	
	    if (C_assigned) {
	        // Check that the assigned C meets the lower bounds.
	        if (C_val < q || C_val < C_min) {
	            cout << "No\n";
	        } else {
	            cout << "Yes\n";
	        }
	    } else {
	        // No exact tail constraint on C, so we can choose C = max(q, C_min)
	        // which will satisfy all op_j >= 0.
	        ll C_pick = max(q, C_min);
	        // It's always possible in this branch.
	        cout << "Yes\n";
	    }
	
	    return 0;
    }
}
0