結果

問題 No.4 おもりと天秤
ユーザー 明智重蔵明智重蔵
提出日時 2015-07-20 15:22:23
言語 C#(csc)
(csc 3.9.0)
結果
TLE  
実行時間 -
コード長 3,925 bytes
コンパイル時間 2,292 ms
コンパイル使用メモリ 107,036 KB
実行使用メモリ 28,996 KB
最終ジャッジ日時 2023-09-22 20:36:12
合計ジャッジ時間 12,365 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 69 ms
22,748 KB
testcase_01 AC 67 ms
22,908 KB
testcase_02 AC 166 ms
26,972 KB
testcase_03 AC 72 ms
22,836 KB
testcase_04 AC 73 ms
24,772 KB
testcase_05 AC 323 ms
28,996 KB
testcase_06 AC 71 ms
22,856 KB
testcase_07 AC 581 ms
27,124 KB
testcase_08 AC 68 ms
22,796 KB
testcase_09 AC 72 ms
24,852 KB
testcase_10 AC 576 ms
27,012 KB
testcase_11 AC 72 ms
22,836 KB
testcase_12 AC 69 ms
22,728 KB
testcase_13 AC 70 ms
24,848 KB
testcase_14 AC 71 ms
24,884 KB
testcase_15 AC 70 ms
22,804 KB
testcase_16 AC 70 ms
22,840 KB
testcase_17 AC 73 ms
24,804 KB
testcase_18 TLE -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
権限があれば一括ダウンロードができます
コンパイルメッセージ
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 = "Input4";

    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> SelectedLeftOmoriIndSet;
        internal HashSet<int> SelectedRightOmoriIndSet;
    }

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

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

        //事前に総和の半分を求めておく
        int OmoriSum = OmoriArr.Sum();

        //総和が奇数ならその時点で解なしとなる
        if (OmoriSum % 2 == 1) {
            Console.WriteLine("impossible");
            return;
        }

        var stk = new Stack<JyoutaiDef>();
        JyoutaiDef WillPush;
        WillPush.CurrP = 0;
        WillPush.SelectedLeftOmoriIndSet = new HashSet<int>() { 0 }; //1つめの重りは必ず左にセット
        WillPush.SelectedRightOmoriIndSet = new HashSet<int>();
        stk.Push(WillPush);

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

            //クリア判定
            int LeftSum; int pRightSum;
            DeriveOmoriSumArr(OmoriArr,
                              Popped.SelectedLeftOmoriIndSet,
                              Popped.SelectedRightOmoriIndSet,
                              out LeftSum, out pRightSum);

            if (LeftSum == OmoriSum / 2) {
                Console.WriteLine("possible");
                return;
            }
            else if (LeftSum > OmoriSum / 2) continue; //総和の半分を超えたら枝切り
            else if (pRightSum > OmoriSum / 2) continue; //総和の半分を超えたら枝切り

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

            //重りを左にセットする経路のPush
            WillPush.CurrP = Popped.CurrP + 1;
            WillPush.SelectedLeftOmoriIndSet = new HashSet<int>(Popped.SelectedLeftOmoriIndSet) { WillPush.CurrP };
            WillPush.SelectedRightOmoriIndSet = Popped.SelectedRightOmoriIndSet;
            stk.Push(WillPush);

            //重りを右にセットする経路のPush
            WillPush.SelectedLeftOmoriIndSet = Popped.SelectedRightOmoriIndSet;
            WillPush.SelectedRightOmoriIndSet = new HashSet<int>(Popped.SelectedRightOmoriIndSet) { WillPush.CurrP };
            stk.Push(WillPush);
        }
        Console.WriteLine("impossible");
    }

    //重りの添字Listを引数として、2つの天秤の重りの合計を求める
    static void DeriveOmoriSumArr(int[] pOmoriArr,
                                  HashSet<int> pSelectedLeftOmoriIndSet,
                                  HashSet<int> pSelectedRightOmoriIndSet,
                                  out int pLeftSum, out int pRightSum)
    {
        pLeftSum = pRightSum = 0;
        for (int I = 0; I <= pOmoriArr.GetUpperBound(0); I++) {
            if (pSelectedLeftOmoriIndSet.Contains(I)) {
                pLeftSum += pOmoriArr[I];
            }
            if (pSelectedRightOmoriIndSet.Contains(I)) {
                pRightSum += pOmoriArr[I];
            }
        }
    }
}
0