結果

問題 No.3143 Colorless Green Parentheses Sleep Furiously
ユーザー Nauclhlt🪷
提出日時 2025-03-09 15:24:23
言語 C#
(.NET 8.0.404)
結果
WA  
(最新)
AC  
(最初)
実行時間 -
コード長 6,033 bytes
コンパイル時間 7,964 ms
コンパイル使用メモリ 169,720 KB
実行使用メモリ 189,668 KB
最終ジャッジ日時 2025-05-17 00:19:09
合計ジャッジ時間 12,358 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 48 WA * 1
権限があれば一括ダウンロードができます
コンパイルメッセージ
  復元対象のプロジェクトを決定しています...
  /home/judge/data/code/main.csproj を復元しました (107 ミリ秒)。
  main -> /home/judge/data/code/bin/Release/net8.0/main.dll
  main -> /home/judge/data/code/bin/Release/net8.0/publish/

ソースコード

diff #

using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Collections;
using System.Text;
using System.Text.RegularExpressions;
using System.Collections.Specialized;
using System.Globalization;
using System.Diagnostics;

StreamWriter writer = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false };
//using StreamWriter writer = new StreamWriter(File.Open("turn.txt", FileMode.Create, FileAccess.ReadWrite));
Console.SetOut(writer);
//using StreamReader reader = new StreamReader(File.Open("in.txt", FileMode.Open));
//Console.SetIn(reader);

Solver.Solve();

Console.Out.Flush();

public static class Solver
{
    private static readonly AtCoderIO cin = new AtCoderIO();

    public static unsafe void Solve()
    {
        int N = cin.Int();
        int K = cin.Int();
        string S = cin.String();

        if (!IsBalanced(S))
        {
            Console.WriteLine("No");
            return;
        }

        // 最小を構築する
        StringBuilder min = new();
        Stack<int> stack = new();

        int sum = 0;
        int[] pair = new int[N];

        for (int i = 0; i < N; i++)
        {
            if (S[i] == '(')
            {
                stack.Push(i);
                min.Append('(');
            }
            else
            {
                int left = stack.Pop();

                pair[left] = i;
                pair[i] = left;

                if (left == i - 1)
                {
                    min.Append("1+1)");
                    sum += 2;
                }
                else
                {
                    min.Append(")");
                }

                if (i < N - 1 && S[i + 1] == '(') min.Append('+');
                if (i < N - 1 && S[i + 1] == ')')
                {
                    min.Append("+1");
                    sum++;
                }
            }
        }

        if (pair[0] == N - 1)
        {
            min.Append("+1");
            sum++;
        }

        if (sum <= K)
        {
            Console.WriteLine("Yes");
            for (int i = 0; i < K - sum; i++)
            {
                min.Append("+1");
            }

            Console.WriteLine(min.ToString());
        }
        else
        {
            Console.WriteLine("No");
        }
    }

    static bool IsBalanced(string s)
    {
        int open = 0;
        for (int i = 0; i < s.Length; i++)
        {
            if (s[i] == '(') open++;
            else
            {
                open--;
            }

            if (open < 0) return false;
        }

        return open == 0;
    }
}

public sealed class AtCoderIO
{
    Queue<string> _readQueue = new Queue<string>();

    private void LoadQueue()
    {
        if (_readQueue.Count > 0) return;
        string line = Console.ReadLine();
        string[] split = line.Split(' ', StringSplitOptions.RemoveEmptyEntries);
        for (int i = 0; i < split.Length; i++) _readQueue.Enqueue(split[i]);
    }

    private void Guard()
    {
        if (_readQueue.Count == 0)
        {
            throw new Exception("NO DATA TO READ");
        }
    }

    public int Int()
    {
        LoadQueue();
        Guard();
        return int.Parse(_readQueue.Dequeue());
    }

    public long Long()
    {
        LoadQueue();
        Guard();
        return long.Parse(_readQueue.Dequeue());
    }

    public string String()
    {
        LoadQueue();
        Guard();
        return _readQueue.Dequeue();
    }

    public short Short()
    {
        LoadQueue();
        Guard();
        return short.Parse(_readQueue.Dequeue());
    }

    public byte Byte()
    {
        LoadQueue();
        Guard();
        return byte.Parse(_readQueue.Dequeue());
    }

    public char Char()
    {
        LoadQueue();
        Guard();
        return char.Parse(_readQueue.Dequeue());
    }

    public double Double()
    {
        LoadQueue();
        Guard();
        return double.Parse(_readQueue.Dequeue());
    }

    public float Float()
    {
        LoadQueue();
        Guard();
        return float.Parse(_readQueue.Dequeue());
    }

    public T Read<T>()
    {
        Type type = typeof(T);
        if (type == typeof(int)) return (T)(object)Int();
        else if (type == typeof(long)) return (T)(object)Long();
        else if (type == typeof(float)) return (T)(object)Float();
        else if (type == typeof(double)) return (T)(object)Double();
        else if (type == typeof(short)) return (T)(object)Short();
        else if (type == typeof(byte)) return (T)(object)Byte();
        else if (type == typeof(char)) return (T)(object)Char();
        else if (type == typeof(string)) return (T)(object)String();
        else return default(T);
    }

    public int[] IntArray(int n)
    {
        if (n == 0) return Array.Empty<int>();

        int[] arr = new int[n];
        for (int i = 0; i < n; i++)
        {
            arr[i] = Int();
        }

        return arr;
    }

    public int[] ZeroIndexedPermutation(int n)
    {
        if (n == 0) return Array.Empty<int>();

        int[] arr = new int[n];
        for (int i = 0; i < n; i++)
        {
            arr[i] = Int() - 1;
        }

        return arr;
    }

    public long[] LongArray(int n)
    {
        if (n == 0) return Array.Empty<long>();

        long[] arr = new long[n];
        for (int i = 0; i < n; i++)
        {
            arr[i] = Long();
        }

        return arr;
    }

    public double[] DoubleArray(int n)
    {
        if (n == 0) return Array.Empty<double>();

        double[] arr = new double[n];
        for (int i = 0; i < n; i++)
        {
            arr[i] = Double();
        }

        return arr;
    }

    public T[] ReadArray<T>(int n)
    {
        if (n == 0) return Array.Empty<T>();

        T[] arr = new T[n];
        for (int i = 0; i < n; i++)
        {
            arr[i] = Read<T>();
        }

        return arr;
    }
}
0