結果

問題 No.78 クジ付きアイスバー
ユーザー ふーらくたる
提出日時 2017-10-13 13:48:44
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 1,266 bytes
コンパイル時間 812 ms
コンパイル使用メモリ 56,800 KB
実行使用メモリ 5,248 KB
最終ジャッジ日時 2024-11-17 11:24:04
合計ジャッジ時間 2,063 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 35
権限があれば一括ダウンロードができます

ソースコード

diff #

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

using int64 = long long;

int main() {
    int N, K;
    cin >> N >> K;
    string S;
    cin >> S;

    const int MAXN = 60;

    static pair<int, int> stamp[MAXN];  // (その時点での使用金額, その時点でのアイスの数)
    for (int i = 0; i < N; i++) stamp[i] = {-1, -1};
    stamp[0] = {0, K};

    int64 ans = 0;
    int ptr = 0;
    // ループを見つけるかINFに入ったら終了
    while (K > 0) {
        ans++;
        K--;

        int start = ptr, ice = S[ptr] - '0';
        ptr = (ptr + 1) % N;

        while (ice > 0 and ptr != start) {
            K--;
            ice--;
            ice += S[ptr] - '0';
            ptr = (ptr + 1) % N;
        }

        /*
        cerr << "ptr is " << ptr << endl;
        cerr << "K is " << K << endl;
        */

        if (ptr == start and ice > 0) K = 0;

        if (K <= 0) break;

        if (stamp[ptr].first >= 0) {
            // ans-stamp[ptr].first円でstamp[ptr].second-K個のアイスを購入できる
            ans += (K / (stamp[ptr].second - K)) * (ans - stamp[ptr].first);
            K %= (stamp[ptr].second - K);
        }
        stamp[ptr] = {ans, K};
    }
    cout << ans << endl;

    return 0;
}
0