結果

問題 No.3017 交互浴
ユーザー 鳩でもわかるC#
提出日時 2025-01-26 17:36:40
言語 C#
(.NET 8.0.404)
結果
WA  
実行時間 -
コード長 1,612 bytes
コンパイル時間 19,525 ms
コンパイル使用メモリ 170,628 KB
実行使用メモリ 222,780 KB
最終ジャッジ日時 2025-01-26 17:37:43
合計ジャッジ時間 62,668 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 7 WA * 48
権限があれば一括ダウンロードができます
コンパイルメッセージ
  復元対象のプロジェクトを決定しています...
  /home/judge/data/code/main.csproj を復元しました (139 ミリ秒)。
  main -> /home/judge/data/code/bin/Release/net8.0/main.dll
  main -> /home/judge/data/code/bin/Release/net8.0/publish/

ソースコード

diff #

using System;
using System.Collections.Generic;
using System.Linq;

class Program
{
    static string ReadLine()
    { 
        return Console.ReadLine().Trim();
    }

    static void Main()
    {
        int N = int.Parse(ReadLine());
        int[] A = ReadLine().Split().Select(_ => int.Parse(_)).ToArray();

        int ans = 0;
        Stack<int> stack = new Stack<int>();
        for (int i = 0; i < N; i++)
        {
            int v = A[i];
            if (i % 2 == 1)
                v *= -1;

            while (stack.Count > 0)
            {
                int p = stack.Peek();
                if (Math.Abs(p) > Math.Abs(v))
                    break;
                int pop = stack.Pop();
                ans -= pop;
                //if (i == 4)
                //{
                //    Console.WriteLine("pop >> " + pop);
                //    Console.WriteLine("peek >> " + stack.Peek());
                //    Console.WriteLine("Math.Abs(v) >> " + Math.Abs(v));
                //}
            }

            stack.Push(v);
            ans += v;

            if (stack.Count >= 2)
            {
                int pop1 = stack.Pop();
                int pop2 = stack.Pop();
                if ((pop1 > 0 && pop2 > 0) || (pop1 < 0 && pop2 < 0))
                {
                    stack.Push(pop2);
                    ans -= pop1;
                }
                else
                {
                    stack.Push(pop2);
                    stack.Push(pop1);
                }
            }

            Console.WriteLine(ans);
        }


        //Console.WriteLine(ans);
    }
}

0