結果
| 問題 |
No.2927 Reverse Polish Equation
|
| コンテスト | |
| ユーザー |
aketijyuuzou
|
| 提出日時 | 2025-02-18 14:00:02 |
| 言語 | C#(csc) (csc 3.9.0) |
| 結果 |
AC
|
| 実行時間 | 1,449 ms / 2,000 ms |
| コード長 | 2,884 bytes |
| コンパイル時間 | 5,408 ms |
| コンパイル使用メモリ | 117,000 KB |
| 実行使用メモリ | 60,052 KB |
| 最終ジャッジ日時 | 2025-02-18 14:00:29 |
| 合計ジャッジ時間 | 27,506 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 43 |
コンパイルメッセージ
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(0, ExpArr) > Y) {
Console.WriteLine(-1);
return;
}
if (ExecCalc(Y, ExpArr) < Y) {
Console.WriteLine(-1);
return;
}
long L = 0;
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<decimal>();
foreach (string EachExp in pExpArr) {
if (EachExp == "+" || EachExp == "min" || EachExp == "max") {
decimal Popped1 = Stk.Pop();
decimal Popped2 = 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);
}
else {
if (EachExp == "X") {
Stk.Push(pX);
}
else {
Stk.Push(decimal.Parse(EachExp));
}
}
}
decimal Result = Stk.Peek();
return Result;
}
}
aketijyuuzou