結果

問題 No.78 クジ付きアイスバー
ユーザー aketijyuuzouaketijyuuzou
提出日時 2024-10-10 22:31:28
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 28 ms / 5,000 ms
コード長 4,341 bytes
コンパイル時間 2,763 ms
コンパイル使用メモリ 115,552 KB
実行使用メモリ 27,112 KB
最終ジャッジ日時 2024-10-10 22:31:34
合計ジャッジ時間 3,688 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 28 ms
24,880 KB
testcase_01 AC 27 ms
22,844 KB
testcase_02 AC 27 ms
26,992 KB
testcase_03 AC 28 ms
24,856 KB
testcase_04 AC 27 ms
22,652 KB
testcase_05 AC 28 ms
26,860 KB
testcase_06 AC 28 ms
24,852 KB
testcase_07 AC 28 ms
24,980 KB
testcase_08 AC 28 ms
25,076 KB
testcase_09 AC 28 ms
24,948 KB
testcase_10 AC 28 ms
24,812 KB
testcase_11 AC 28 ms
24,972 KB
testcase_12 AC 27 ms
22,904 KB
testcase_13 AC 28 ms
24,884 KB
testcase_14 AC 28 ms
24,856 KB
testcase_15 AC 27 ms
24,692 KB
testcase_16 AC 27 ms
24,820 KB
testcase_17 AC 28 ms
24,944 KB
testcase_18 AC 28 ms
24,820 KB
testcase_19 AC 27 ms
27,112 KB
testcase_20 AC 28 ms
24,816 KB
testcase_21 AC 28 ms
24,752 KB
testcase_22 AC 28 ms
26,888 KB
testcase_23 AC 28 ms
24,848 KB
testcase_24 AC 27 ms
26,988 KB
testcase_25 AC 28 ms
24,972 KB
testcase_26 AC 27 ms
24,876 KB
testcase_27 AC 28 ms
26,988 KB
testcase_28 AC 28 ms
25,008 KB
testcase_29 AC 27 ms
22,960 KB
testcase_30 AC 28 ms
27,020 KB
testcase_31 AC 27 ms
24,816 KB
testcase_32 AC 28 ms
26,892 KB
testcase_33 AC 28 ms
22,708 KB
testcase_34 AC 27 ms
25,004 KB
testcase_35 AC 28 ms
26,884 KB
testcase_36 AC 27 ms
26,856 KB
testcase_37 AC 28 ms
26,884 KB
testcase_38 AC 27 ms
24,944 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc)
Copyright (C) Microsoft Corporation. All rights reserved.

ソースコード

diff #

using System;
using System.Collections.Generic;
using System.Linq;

class Program
{
    static string InputPattern = "InputX";

    static List<string> GetInputList()
    {
        var WillReturn = new List<string>();

        if (InputPattern == "Input1") {
            WillReturn.Add("5 4");
            WillReturn.Add("01200");
            //2
            //1つの箱の中には5個のアイスバーが入っている。
            //先頭から順に「ハズレ」、「1本当たり」、「2本当たり」、「ハズレ」、「ハズレ」である。
            //最初の2本のアイスバーは購入しなければならない。
            //2本目のアイスバーは「1本当たり」であるので3本目のアイスバーはタダでもらえる。
            //3本目のアイスバーは「2本当たり」であるので4,5本目のアイスバーもタダでもらえる。
            //A君が食べたいアイスバーの数は4本なのでこの時点で目的は達成した。
            //A君が購入したアイスバーは2本である。
        }
        else if (InputPattern == "Input2") {
            WillReturn.Add("2 8");
            WillReturn.Add("10");
            //4
            //1つのアイスバーの箱に2本のアイスバーが入っている。
            //箱には「1本当たり」、「ハズレ」の順番でアイスバーが入っている。
            //1本買うたびに1本もらえるので1本買うと2本食べれると考える。
            //よって、8本食べるには4本の購入が必要となる。
        }
        else if (InputPattern == "Input3") {
            WillReturn.Add("10 100");
            WillReturn.Add("2222222222");
            //1
            //1つの箱に「2本当たり」のアイスバーが10本入っている。
            //当たりを使ってアイスバーをもらい放題だが最初の1本はどうしても買う必要がある。
        }
        else if (InputPattern == "Input4") {
            WillReturn.Add("2 5");
            WillReturn.Add("01");
            //3
            //1つのアイスバーの箱に2本のアイスバーが入っている。
            //箱には「ハズレ」、「1本当たり」の順番でアイスバーが入っている。
            //5本のアイスバーを食べるには3本のアイスバーの購入が必要である。
        }
        else {
            string wkStr;
            while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
        }
        return WillReturn;
    }

    static int K;
    static char[] SArr;

    static void Main()
    {
        List<string> InputList = GetInputList();
        K = int.Parse(InputList[0].Split(' ').Last());
        SArr = InputList[1].ToCharArray();

        Console.WriteLine(Solve());
    }

    static int Solve()
    {
        int HonsuuSum = 0;
        int KounyuuCnt = 0;
        int AtariHonsuu = 0;

        //1箱分の処理を行う
        Action HitohakoSyori = () =>
        {
            foreach (char EachChar in SArr) {
                if (AtariHonsuu == 0) KounyuuCnt++;
                else AtariHonsuu--;

                if (EachChar == '1') AtariHonsuu++;
                if (EachChar == '2') AtariHonsuu += 2;

                if (++HonsuuSum == K) {
                    return;
                }
            }
        };

        //1箱目の処理を行う
        HitohakoSyori();

        //1箱目で指定本数に達した場合
        if (HonsuuSum == K) {
            return KounyuuCnt;
        }

        //持ち越した当たり本数を使っての
        //次の箱での購入数
        int SecondBoxKounyuuCnt = KounyuuCnt - AtariHonsuu;
        if (SecondBoxKounyuuCnt <= 0) return KounyuuCnt;

        //指定本数までの全箱数
        int TotalBoxCnt = K / SArr.Length;
        if (K % SArr.Length > 0) TotalBoxCnt++;

        //1箱目と最終箱以外の箱数
        int RestBoxCnt = TotalBoxCnt - 2;

        //1箱目と最終箱以外の箱を一気に処理
        if (RestBoxCnt > 0) {
            HonsuuSum += RestBoxCnt * SArr.Length;
            KounyuuCnt += RestBoxCnt * SecondBoxKounyuuCnt;
        }

        //最終箱の処理を行う
        HitohakoSyori();
        return KounyuuCnt;
    }
}

0