結果

問題 No.1211 円環はお断り
ユーザー msm1993
提出日時 2020-10-08 10:19:41
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 874 ms / 2,000 ms
コード長 1,615 bytes
コンパイル時間 1,925 ms
コンパイル使用メモリ 172,076 KB
実行使用メモリ 20,480 KB
最終ジャッジ日時 2024-07-20 05:51:45
合計ジャッジ時間 18,007 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 41
権限があれば一括ダウンロードができます

ソースコード

diff #

// 解法見て書いただけ 要復習

/**
 *   @FileName	a.cpp
 *   @Author	kanpurin
 *   @Created	2020.09.01 04:06:21
**/

#include "bits/stdc++.h" 
using namespace std; 
typedef long long ll;

int n,k;
vector<int> a;

bool f(ll x) {
    vector<vector<int>> next(20,vector<int>(2 * n + 1));
    ll sum = 0;
    int t = 0;
    for (int i = 0; i < n; i++) {
        while(t < 2 * n && sum < x) {
            sum += a[t % n];
            t++;
            if (i + n <= t) return false;
        }
        next[0][i] = t;
        next[0][i + n] = min(2 * n,t + n);
        sum -= a[i];
    }
    for (int i = 0; i < 20; i++) {
        next[i][2 * n] = 2 * n;
    } 
    for (int i = 0; i < 19; i++) {
        for (int j = 0; j < n; j++) {
            next[i + 1][j] = next[i][next[i][j]];
            next[i + 1][j + n] = next[i][next[i][j + n]];
        }
    }
    for (int i = 0; i < n; i++) {
        int p = k;
        int q = 0;
        int now = i;
        while(p) {
            if (p & 1) {
                now = next[q][now];
            }
            p >>= 1;
            q++;
        }
        if (now <= n + i) return true;
    }
    return false;
}

int main() {
    cin >> n >> k;
    a.resize(2 * n);
    ll l = 0, r = 1;
    for (int i = 0; i < n; i++) {
        cin >> a[i];
        a[i + n] = a[i];
        r += a[i];
    }
    if (k == 1) {
        cout << r - 1 << endl;
        return 0;
    }
    while(r - l > 1) {
        ll mid = l + (r - l) / 2;
        if (f(mid)) {
            l = mid;
        }
        else {
            r = mid;
        }
    }
    cout << l << endl;
    return 0;
}
0