結果

問題 No.1594 Three Classes
ユーザー bluemegane
提出日時 2021-07-10 10:56:56
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 408 ms / 2,000 ms
コード長 1,225 bytes
コンパイル時間 1,685 ms
コンパイル使用メモリ 106,112 KB
実行使用メモリ 85,216 KB
最終ジャッジ日時 2024-11-16 08:25:53
合計ジャッジ時間 6,951 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 18
権限があれば一括ダウンロードができます
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc)
Copyright (C) Microsoft Corporation. All rights reserved.

ソースコード

diff #

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

public class P
{
    public int t { get; set; }
    public string s { get; set; }
}

public class Hello
{
    static void Main()
    {
        var n = int.Parse(Console.ReadLine().Trim());
        string[] line = Console.ReadLine().Trim().Split(' ');
        var a = Array.ConvertAll(line, int.Parse);
        if (a.Sum() % 3 != 0) Console.WriteLine("No");
        else getAns(n, a);
    }
    static void getAns(int n, int[] a)
    {
        var q = new Queue<P>();
        q.Enqueue(new P { s = "", t = 0 });
        while (q.Count() > 0)
        {
            var w = q.Dequeue();
            if (w.t == n )
            {
                if (check(w.s, n, a)) { Console.WriteLine("Yes"); return; }
                else continue;
            }
            for (int i = 0; i < 3; i++)
            {
                q.Enqueue(new P { s = w.s + i.ToString(), t = w.t + 1 });
            }
        }
        Console.WriteLine("No");
    }
    static bool check(string s, int n, int[] a)
    {
        var sum = new int[3];
        for (int i = 0; i < n; i++)
            sum[s[i] - '0'] += a[i];
        return (sum[0] == sum[1] && sum[1] == sum[2]);
    }
}
0