結果

問題 No.4 おもりと天秤
ユーザー 明智重蔵明智重蔵
提出日時 2015-07-21 20:08:39
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 74 ms / 5,000 ms
コード長 2,697 bytes
コンパイル時間 2,248 ms
コンパイル使用メモリ 110,816 KB
実行使用メモリ 24,008 KB
最終ジャッジ日時 2023-09-08 16:21:30
合計ジャッジ時間 4,823 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 65 ms
21,876 KB
testcase_01 AC 62 ms
21,924 KB
testcase_02 AC 63 ms
23,920 KB
testcase_03 AC 65 ms
24,004 KB
testcase_04 AC 64 ms
22,004 KB
testcase_05 AC 70 ms
21,828 KB
testcase_06 AC 64 ms
21,836 KB
testcase_07 AC 71 ms
21,872 KB
testcase_08 AC 65 ms
23,764 KB
testcase_09 AC 65 ms
21,884 KB
testcase_10 AC 70 ms
23,864 KB
testcase_11 AC 64 ms
21,912 KB
testcase_12 AC 62 ms
21,848 KB
testcase_13 AC 62 ms
21,824 KB
testcase_14 AC 64 ms
21,892 KB
testcase_15 AC 63 ms
21,916 KB
testcase_16 AC 65 ms
19,856 KB
testcase_17 AC 64 ms
21,876 KB
testcase_18 AC 74 ms
21,820 KB
testcase_19 AC 70 ms
23,876 KB
testcase_20 AC 69 ms
24,008 KB
testcase_21 AC 69 ms
23,884 KB
testcase_22 AC 69 ms
21,916 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc)
Copyright (C) Microsoft Corporation. All rights reserved.

ソースコード

diff #

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

class Program
{
    static string InputPattern = "Input5";

    static List<string> GetInputList()
    {
        var WillReturn = new List<string>();

        if (InputPattern == "Input1") {
            WillReturn.Add("3");
            WillReturn.Add("1 2 3");
        }
        else if (InputPattern == "Input2") {
            WillReturn.Add("5");
            WillReturn.Add("1 2 3 4 5");
        }
        else if (InputPattern == "Input3") {
            WillReturn.Add("15");
            WillReturn.Add("62 8 90 2 24 62 38 64 76 60 30 76 80 74 72");
        }
        else if (InputPattern == "Input4") {
            WillReturn.Add("100");
            WillReturn.AddRange(Enumerable.Repeat("100", 100));
        }
        else {
            string wkStr;
            while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
        }
        return WillReturn;
    }

    struct JyoutaiDef
    {
        internal int CurrP;
        internal bool[] CanCreateSum;
    }

    static void Main()
    {
        List<string> InputList = GetInputList();

        int[] OmoriArr =
            InputList.Skip(1).SelectMany(X => X.Split(' '), (X, Y) => int.Parse(Y)).ToArray();

        //事前に総合計を求めておく
        int OmoriSum = OmoriArr.Sum();

        //総合計が奇数なら解なし
        if (OmoriSum % 2 == 1) {
            Console.WriteLine("impossible");
            return;
        }

        var stk = new Stack<JyoutaiDef>();
        JyoutaiDef WillPush;
        WillPush.CurrP = 0;
        WillPush.CanCreateSum = new bool[OmoriSum / 2 + 1];
        WillPush.CanCreateSum[0] = true;
        stk.Push(WillPush);

        bool FoundAnswer = false;
        while (stk.Count > 0) {
            JyoutaiDef Popped = stk.Pop();

            //クリア判定
            if (Popped.CanCreateSum[OmoriSum / 2]) {
                FoundAnswer = true;
                break;
            }

            if (Popped.CurrP >= OmoriArr.GetUpperBound(0))
                continue;

            WillPush.CurrP = Popped.CurrP + 1;
            WillPush.CanCreateSum = (bool[])Popped.CanCreateSum.Clone();

            for (int I = WillPush.CanCreateSum.GetUpperBound(0); 0 <= I; I--) {
                if (WillPush.CanCreateSum[I] == false)
                    continue;

                int wkInd = I + OmoriArr[WillPush.CurrP];
                if (wkInd <= WillPush.CanCreateSum.GetUpperBound(0)) {
                    WillPush.CanCreateSum[wkInd] = true;
                }
            }
            stk.Push(WillPush);
        }
        Console.WriteLine(FoundAnswer ? "possible" : "impossible");
    }
}
0