結果

問題 No.4 おもりと天秤
ユーザー 明智重蔵明智重蔵
提出日時 2015-07-20 14:51:23
言語 C#(csc)
(csc 3.9.0)
結果
WA  
実行時間 -
コード長 3,565 bytes
コンパイル時間 3,540 ms
コンパイル使用メモリ 108,808 KB
実行使用メモリ 24,928 KB
最終ジャッジ日時 2023-09-22 20:35:10
合計ジャッジ時間 4,909 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 65 ms
22,700 KB
testcase_01 WA -
testcase_02 WA -
testcase_03 AC 66 ms
24,740 KB
testcase_04 AC 64 ms
22,696 KB
testcase_05 AC 65 ms
22,680 KB
testcase_06 WA -
testcase_07 AC 65 ms
22,808 KB
testcase_08 WA -
testcase_09 AC 65 ms
24,700 KB
testcase_10 AC 65 ms
22,776 KB
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 AC 64 ms
22,732 KB
testcase_17 AC 64 ms
24,748 KB
testcase_18 WA -
testcase_19 AC 66 ms
22,784 KB
testcase_20 AC 65 ms
22,772 KB
testcase_21 AC 65 ms
22,692 KB
testcase_22 AC 66 ms
24,832 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 = "Input1";

    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 {
            string wkStr;
            while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
        }
        return WillReturn;
    }

    struct JyoutaiDef
    {
        internal int CurrP;
        internal HashSet<int> SelectedOmoriIndSet;
    }

    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.SelectedOmoriIndSet = new HashSet<int>() { 0 }; //1つめの重りは必ず選択するとする
        stk.Push(WillPush);

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

            //クリア判定
            int LeftOmoriSum = DeriveOmoriSum(OmoriArr, Popped.SelectedOmoriIndSet);

            if (LeftOmoriSum == OmoriSum / 2) {
                //Console.WriteLine("解を発見しました");
                //Console.WriteLine("選択する重りは");
                foreach (int EachInd in Popped.SelectedOmoriIndSet) {
                    //Console.WriteLine(OmoriArr[EachInd]);
                }
                //Console.WriteLine("選択しない重りは");
                for (int I = 0; I <= OmoriArr.GetUpperBound(0); I++) {
                    if (Popped.SelectedOmoriIndSet.Contains(I)) continue;
                    //Console.WriteLine(OmoriArr[I]);
                }
                Console.WriteLine("possible");
                return;
            }
            else if (LeftOmoriSum > OmoriSum / 2) {
                continue; //総和の半分を超えたら枝切り
            }

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

            //重りを選択する経路のPush
            WillPush.CurrP = Popped.CurrP + 1;
            WillPush.SelectedOmoriIndSet = new HashSet<int>(Popped.SelectedOmoriIndSet) { WillPush.CurrP };
            stk.Push(WillPush);

            //重りを選択しない経路のPush
            WillPush.SelectedOmoriIndSet = Popped.SelectedOmoriIndSet;
            stk.Push(WillPush);
        }
        Console.WriteLine("impossible");
    }

    //重りの添字Listを引数として、重りの合計を求める
    static int DeriveOmoriSum(int[] pOmoriArr, HashSet<int> pSelectedOmoriIndSet)
    {
        int WillReturn = 0;
        for (int I = 0; I <= pOmoriArr.GetUpperBound(0); I++) {
            if (pSelectedOmoriIndSet.Contains(I)) {
                WillReturn += pOmoriArr[I];
            }
        }
        return WillReturn;
    }
}
0