結果

問題 No.505 カードの数式2
ユーザー 14番14番
提出日時 2017-04-22 00:54:09
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 35 ms / 2,000 ms
コード長 2,712 bytes
コンパイル時間 926 ms
コンパイル使用メモリ 114,368 KB
実行使用メモリ 27,668 KB
最終ジャッジ日時 2024-04-26 17:40:47
合計ジャッジ時間 3,115 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 34 ms
27,280 KB
testcase_01 AC 34 ms
25,464 KB
testcase_02 AC 34 ms
25,344 KB
testcase_03 AC 32 ms
25,600 KB
testcase_04 AC 33 ms
27,544 KB
testcase_05 AC 34 ms
27,496 KB
testcase_06 AC 34 ms
27,660 KB
testcase_07 AC 34 ms
27,548 KB
testcase_08 AC 33 ms
25,472 KB
testcase_09 AC 33 ms
25,204 KB
testcase_10 AC 34 ms
25,332 KB
testcase_11 AC 33 ms
25,596 KB
testcase_12 AC 34 ms
27,284 KB
testcase_13 AC 33 ms
23,476 KB
testcase_14 AC 34 ms
25,500 KB
testcase_15 AC 34 ms
25,460 KB
testcase_16 AC 34 ms
25,588 KB
testcase_17 AC 34 ms
25,456 KB
testcase_18 AC 35 ms
27,536 KB
testcase_19 AC 34 ms
27,284 KB
testcase_20 AC 34 ms
25,360 KB
testcase_21 AC 35 ms
25,204 KB
testcase_22 AC 35 ms
27,548 KB
testcase_23 AC 34 ms
25,500 KB
testcase_24 AC 34 ms
25,456 KB
testcase_25 AC 34 ms
25,336 KB
testcase_26 AC 34 ms
27,292 KB
testcase_27 AC 34 ms
27,668 KB
testcase_28 AC 34 ms
25,216 KB
testcase_29 AC 34 ms
27,244 KB
testcase_30 AC 34 ms
25,464 KB
testcase_31 AC 34 ms
25,248 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