結果

問題 No.78 クジ付きアイスバー
ユーザー fantasiabaetica
提出日時 2018-06-29 15:43:17
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 1,253 bytes
コンパイル時間 616 ms
コンパイル使用メモリ 65,488 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-06-30 23:47:51
合計ジャッジ時間 1,771 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 35
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
typedef long long int ll;
using namespace std;

ll n, k;
int cost = 0;
int stock = 0;
char box[50];

void buy_box(){
    for (int i = 0; i < n; i++){
        int ice = box[i];
        if (stock > 0){
            stock -= 1;
        } else {
            cost += 1;
        }
        if (ice == 2){
            stock += 2;
        } else if (ice == 1){
            stock += 1;
        } 
    }
}

int buy(int rest){
    int cost_rest = 0;
    for (int i = 0; i < rest; i++){
        int ice = box[i];
        if (stock > 0){
            stock -= 1;
        } else {
            cost_rest += 1;
        }
        if (ice == 2){
            stock += 2;
        } else if (ice == 1){
            stock += 1;
        } 
    }
    return cost_rest;
}

int main(){
    cin >> n >> k;
    string ices;
    cin >> ices;
    for (int i = 0; i < n; i++){
        int ice = ices[i] - '0';
        box[i] = ice;
    }
    if (n > k){
        cout << buy(k) << endl;    
        return 0;
    }
    buy_box();
    if (cost <= stock){
        cout << cost << endl;
    } else {
        int cost_sum = cost;
        cost_sum += (k / n - 1) * (cost - stock);
        cost_sum += buy(k % n);
        cout << cost_sum << endl;
    }
    
    return 0;
}
0