結果

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

ソースコード

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(vector<ll> &A, int index, ll &ans){
    ll cur_up = 0;
    ll sousa = 0;
    ans = A[index];
    for(int i = index + 1; i <= N; i++){
        ll cur_num = A[i] + cur_up;
        if(cur_num >= A[index]) continue;
        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 += (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};
    {
        int 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;
        }
        assert(canMakeMin(A, min_kouho[r_index], ans));
        cout << ans << "\n";
    }
}
0