結果

問題 No.2217 Suffix+
ユーザー CoCo_Japan_pan
提出日時 2023-02-17 23:57:27
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 2,058 bytes
コンパイル時間 1,567 ms
コンパイル使用メモリ 197,860 KB
最終ジャッジ日時 2025-02-10 18:26:09
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 25 WA * 8
権限があれば一括ダウンロードができます

ソースコード

diff #

// 提出時にassertはオフ
#ifndef DEBUG
#ifndef NDEBUG
#define NDEBUG
#endif
#endif

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

#define ALL(x) (x).begin(), (x).end()
template <class T> using vec = vector<T>;

int N, K;

// i番目を全体の最小値にすることがK回以下の操作で可能ならtrue
bool canMakeMin(const vector<ll> &A, int index, ll &ans){
    ll cur_up = 0;
    ll sousa = 0;
    // 各A[i]がA[index]以上になるよう操作
    for(int i = index + 1; i <= N; i++){
        ll cur_num = A[i] + cur_up;
        if(cur_num >= A[index]) continue;
        // [i, N]をcur_sousa回だけ+i
        ll cur_sousa = (A[index] - cur_num - 1) / i + 1;
        cur_up += cur_sousa * i;
        sousa += cur_sousa;
        // K回では足りない
        if(sousa > K) return false;
    }
    // この時の答も求めておく
    ans = A[index] + (K - sousa) * index;
    return true;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cin >> N >> K;
    vec<ll> A(N + 1);
    for(int i = 1; i <= N; i++) cin >> A[i];

    // 全体の最小値にするindexの候補 [1, i)のminよりA[i]が小さければOK
    vec<int> min_kouho = {1};
    {
        ll cur_min = A[1];
        for(int i = 2; i <= N; i++) {
            if(A[i] < cur_min){
                min_kouho.push_back(i);
                cur_min = A[i];
            }
        }
    }
    
    // min_kouhoのうち、操作K回で実際に最小値にできる最小のindexを二分探索で求める
    ll ans = 0;
    // 先頭を全体の最小値にできるとき
    if(canMakeMin(A, 1, ans)){
        cout << ans << "\n";
        return 0;
    }

    // l_indexはNG, r_indexはOK
    int l_index = 0;
    int r_index = min_kouho.size() - 1;
    while(r_index - l_index > 1) {
        int m_index = (l_index + r_index) / 2;
        if(canMakeMin(A, min_kouho[m_index], ans)) r_index = m_index;
        else l_index = m_index;
    }
    canMakeMin(A, min_kouho[r_index], ans);
    cout << ans << "\n";
}
0