結果
問題 |
No.2927 Reverse Polish Equation
|
ユーザー |
![]() |
提出日時 | 2025-02-18 13:46:19 |
言語 | C#(csc) (csc 3.9.0) |
結果 |
TLE
|
実行時間 | - |
コード長 | 2,827 bytes |
コンパイル時間 | 6,398 ms |
コンパイル使用メモリ | 117,252 KB |
実行使用メモリ | 49,428 KB |
最終ジャッジ日時 | 2025-02-18 13:46:45 |
合計ジャッジ時間 | 24,297 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 13 TLE * 3 -- * 27 |
コンパイルメッセージ
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.Linq; // https://yukicoder.me/problems/no/2927 class Program { static string InputPattern = "InputX"; static List<string> GetInputList() { var WillReturn = new List<string>(); if (InputPattern == "Input1") { WillReturn.Add("3 5"); WillReturn.Add("X 2 +"); //3 } else if (InputPattern == "Input2") { WillReturn.Add("7 9"); WillReturn.Add("X X 3 max + 11 min"); //-1 } else if (InputPattern == "Input3") { WillReturn.Add("11 10"); WillReturn.Add("X X X X X + + + + 10 max"); //0 } else { string wkStr; while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr); } return WillReturn; } static void Main() { List<string> InputList = GetInputList(); long[] wkArr = InputList[0].Split(' ').Select(pX => long.Parse(pX)).ToArray(); long Y = wkArr[1]; string[] ExpArr = InputList[1].Split(' '); if (ExecCalc(0, ExpArr) == Y) { Console.WriteLine(0); return; } if (ExecCalc(Y, ExpArr) < Y) { Console.WriteLine(-1); return; } long L = 1; long R = Y; while (L + 1 < R) { long Mid = (L + R) / 2; if (ExecCalc(Mid,ExpArr)>=Y) { R = Mid; } else { L = Mid; } } decimal Result = ExecCalc(R, ExpArr); if (Result == Y) { Console.WriteLine(R); } else { Console.WriteLine(-1); } } // Xの値と、文字列を引数として、計算結果を返す static decimal ExecCalc(long pX, string[] pExpArr) { var Stk = new Stack<string>(); foreach (string EachExp in pExpArr) { if (EachExp == "+" || EachExp == "min" || EachExp == "max") { decimal Popped1 = decimal.Parse(Stk.Pop()); decimal Popped2 = decimal.Parse(Stk.Pop()); decimal CalcResult = 0; if (EachExp == "+") CalcResult = Popped1 + Popped2; if (EachExp == "min") CalcResult = Math.Min(Popped1, Popped2); if (EachExp == "max") CalcResult = Math.Max(Popped1, Popped2); Stk.Push(CalcResult.ToString()); } else { if (EachExp == "X") { Stk.Push(pX.ToString()); } else { Stk.Push(EachExp); } } } decimal Result = decimal.Parse(Stk.Peek()); return Result; } }