結果
問題 |
No.505 カードの数式2
|
ユーザー |
![]() |
提出日時 | 2017-04-22 00:54:09 |
言語 | C#(csc) (csc 3.9.0) |
結果 |
AC
|
実行時間 | 35 ms / 2,000 ms |
コード長 | 2,712 bytes |
コンパイル時間 | 948 ms |
コンパイル使用メモリ | 113,372 KB |
実行使用メモリ | 27,648 KB |
最終ジャッジ日時 | 2024-11-14 07:20:45 |
合計ジャッジ時間 | 2,953 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 29 |
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc) Copyright (C) Microsoft Corporation. All rights reserved.
ソースコード
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(); } }