結果

問題 No.2591 安上がりな括弧列
ユーザー aketijyuuzou
提出日時 2025-02-20 22:30:23
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 265 ms / 2,000 ms
コード長 2,618 bytes
コンパイル時間 2,646 ms
コンパイル使用メモリ 114,156 KB
実行使用メモリ 53,080 KB
最終ジャッジ日時 2025-02-20 22:30:30
合計ジャッジ時間 6,613 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 23
権限があれば一括ダウンロードができます
コンパイルメッセージ
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;

// https://yukicoder.me/problems/no/2591
class Program
{
    static string InputPattern = "InputX";

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

        if (InputPattern == "Input1") {
            WillReturn.Add("3");
            WillReturn.Add(")(((()");
            WillReturn.Add("10 22 3 5 2 13");
            //15
        }
        else if (InputPattern == "Input2") {
            WillReturn.Add("4");
            WillReturn.Add("()(()())");
            WillReturn.Add("3141592 653589 793238 462643 38327 9502884 1971 69399375");
            //0
        }
        else if (InputPattern == "Input3") {
            WillReturn.Add("5");
            WillReturn.Add("((((((((((");
            WillReturn.Add("1 2 3 4 5 6 7 8 9 10");
            //30
        }
        else {
            string wkStr;
            while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
        }
        return WillReturn;
    }

    static void Main()
    {
        List<string> InputList = GetInputList();
        char[] CharArr = InputList[1].ToCharArray();
        long[] CostArr = InputList[2].Split(' ').Select(pX => long.Parse(pX)).ToArray();
        long UB = CharArr.GetUpperBound(0);

        // コスト[括弧の深さ]なDP表
        var PrevDP = new Dictionary<long, long>();
        PrevDP[0] = 0;

        for (long I = 0; I <= UB; I++) {
            var CurrDP = new Dictionary<long, long>();
            foreach (var EachPair in PrevDP) {
                Action<char, long> UpdateAct = (pNewChar, pCost) =>
                {
                    long NewKey = EachPair.Key;
                    if (pNewChar == ')') {
                        if (--NewKey < 0) {
                            return;
                        }
                    }
                    else NewKey++;

                    long NewVal = EachPair.Value + pCost;
                    if (CurrDP.ContainsKey(NewKey)) {
                        if (CurrDP[NewKey] <= NewVal) {
                            return;
                        }
                    }
                    CurrDP[NewKey] = NewVal;
                };

                if (CharArr[I] == '(') {
                    UpdateAct(')', CostArr[I]);
                    UpdateAct('(', 0);
                }
                if (CharArr[I] == ')') {
                    UpdateAct('(', CostArr[I]);
                    UpdateAct(')', 0);
                }
            }
            PrevDP = CurrDP;
        }
        Console.WriteLine(PrevDP[0]);
    }
}
0