結果

問題 No.78 クジ付きアイスバー
ユーザー otsnskotsnsk
提出日時 2014-12-25 01:13:52
言語 C++11
(gcc 13.3.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 1,002 bytes
コンパイル時間 613 ms
コンパイル使用メモリ 60,504 KB
実行使用メモリ 5,248 KB
最終ジャッジ日時 2024-11-08 00:27:30
合計ジャッジ時間 1,618 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 35
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
using namespace std;

inline int first_pos(vector<int> &v, int last, int val) {
    auto b = v.begin();
    return distance(b, lower_bound(b, b+last, val));
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);

    int N, K;
    string S;
    cin >> N >> K;
    cin >> S;

    vector<int> buy(55);
    int carry = 0, b;
    auto bar = S.cbegin();
    buy[0] = 0;
    for (b = 1; b <= N; b++) {
        buy[b] = buy[b-1];
        int g = 1;
        while (g--) {
            buy[b]++;
            g += (int)(*bar - '0');
            if (++bar == S.cend()) {
                carry = g;
                goto OUT;
            }
        }
    }
    OUT:;

    if (K < N) {
        cout << first_pos(buy, b, K) << endl;
    }
    else if (carry >= b) {
        cout << b << endl;
    }
    else {
        int rcost = b - carry;
        cout << b + (K-N)/N*rcost + max(0, first_pos(buy, b, K%N)-carry)
             << endl;
        
    }
    
    return 0;
}
0