結果

問題 No.505 カードの数式2
ユーザー 14番14番
提出日時 2017-04-22 00:54:09
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 67 ms / 2,000 ms
コード長 2,712 bytes
コンパイル時間 2,282 ms
コンパイル使用メモリ 107,868 KB
実行使用メモリ 23,828 KB
最終ジャッジ日時 2023-08-09 02:05:05
合計ジャッジ時間 5,652 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 64 ms
21,676 KB
testcase_01 AC 66 ms
23,828 KB
testcase_02 AC 66 ms
21,712 KB
testcase_03 AC 66 ms
21,808 KB
testcase_04 AC 65 ms
21,620 KB
testcase_05 AC 64 ms
21,700 KB
testcase_06 AC 65 ms
21,576 KB
testcase_07 AC 65 ms
23,784 KB
testcase_08 AC 64 ms
21,764 KB
testcase_09 AC 66 ms
21,600 KB
testcase_10 AC 64 ms
21,732 KB
testcase_11 AC 65 ms
21,696 KB
testcase_12 AC 65 ms
21,620 KB
testcase_13 AC 67 ms
21,684 KB
testcase_14 AC 65 ms
23,732 KB
testcase_15 AC 65 ms
19,760 KB
testcase_16 AC 65 ms
21,776 KB
testcase_17 AC 66 ms
21,612 KB
testcase_18 AC 66 ms
23,728 KB
testcase_19 AC 65 ms
21,748 KB
testcase_20 AC 67 ms
23,748 KB
testcase_21 AC 65 ms
19,616 KB
testcase_22 AC 64 ms
19,760 KB
testcase_23 AC 66 ms
19,576 KB
testcase_24 AC 65 ms
21,748 KB
testcase_25 AC 64 ms
21,740 KB
testcase_26 AC 65 ms
21,640 KB
testcase_27 AC 65 ms
23,744 KB
testcase_28 AC 65 ms
21,708 KB
testcase_29 AC 65 ms
21,804 KB
testcase_30 AC 65 ms
21,668 KB
testcase_31 AC 65 ms
21,572 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.Text;
using System.Linq;

class Program
{
    public void Proc()
    {
        Reader.IsDebug = false;
        int itemCount = int.Parse(Reader.ReadLine());
        this.Items = Reader.ReadLine().Split(' ').Select(a => int.Parse(a)).ToArray();
        this.GetAns(1, this.Items[0]);
        Console.WriteLine(this.dic[itemCount - 1][true]);


    }

    private void GetAns(int idx, long subTotal)
    {
        if (idx >= this.Items.Length)
        {
            if (dic.ContainsKey(idx))
            {
                dic[idx][true] = Math.Max(dic[idx][true], subTotal);
                dic[idx][false] = Math.Min(dic[idx][false], subTotal);
            }
            else
            {
                dic.Add(idx, new Dictionary<bool, long>());
                dic[idx][true] = subTotal;
                dic[idx][false] = subTotal;
            }
            return;
        }
        long min = long.MaxValue;
        long max = long.MinValue;

        int num = Items[idx];

        min = Math.Min(min, subTotal + num);
        min = Math.Min(min, subTotal - num);
        min = Math.Min(min, subTotal * num);

        max = Math.Max(max, subTotal + num);
        max = Math.Max(max, subTotal - num);
        max = Math.Max(max, subTotal * num);

        if (num != 0)
        {
            min = Math.Min(min, subTotal / num);
            max = Math.Max(max, subTotal / num);
        }
        if (!dic.ContainsKey(idx))
        {
            dic.Add(idx, new Dictionary<bool, long>());
        }
        if((!dic[idx].ContainsKey(true)) || (dic[idx][true] < max)) {
            dic[idx][true] = max;
            this.GetAns(idx + 1, max);
        }
        if ((!dic[idx].ContainsKey(false)) || dic[idx][false] > min)
        {
            dic[idx][false] = min;
            GetAns(idx + 1, min);
        }
    }

    private int[] Items;

    private Dictionary<int, Dictionary<bool, long>> dic = new Dictionary<int, Dictionary<bool, long>>();


    public class Reader
    {
        public static bool IsDebug = true;
        private static String PlainInput = @"


16
6 9 1 2 1 2 1 2 1 4 1 4 1 8 1 2



";
        private static System.IO.StringReader Sr = null;
        public static string ReadLine()
        {
            if (IsDebug)
            {
                if (Sr == null)
                {
                    Sr = new System.IO.StringReader(PlainInput.Trim());
                }
                return Sr.ReadLine();
            }
            else
            {
                return Console.ReadLine();
            }
        }
    }
    static void Main()
    {
        Program prg = new Program();
        prg.Proc();
    }
}
0