結果

問題 No.4 おもりと天秤
ユーザー 明智重蔵明智重蔵
提出日時 2015-07-22 19:17:09
言語 C#(csc)
(csc 3.9.0)
結果
TLE  
実行時間 -
コード長 3,462 bytes
コンパイル時間 5,046 ms
コンパイル使用メモリ 108,036 KB
実行使用メモリ 34,700 KB
最終ジャッジ日時 2023-09-22 21:16:58
合計ジャッジ時間 10,606 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 63 ms
23,900 KB
testcase_01 AC 61 ms
19,720 KB
testcase_02 AC 82 ms
23,888 KB
testcase_03 AC 63 ms
21,748 KB
testcase_04 AC 64 ms
23,988 KB
testcase_05 AC 64 ms
23,760 KB
testcase_06 AC 63 ms
19,816 KB
testcase_07 AC 63 ms
21,868 KB
testcase_08 AC 61 ms
23,808 KB
testcase_09 AC 65 ms
21,860 KB
testcase_10 AC 64 ms
19,828 KB
testcase_11 AC 64 ms
23,848 KB
testcase_12 AC 65 ms
23,864 KB
testcase_13 AC 63 ms
21,716 KB
testcase_14 AC 63 ms
23,804 KB
testcase_15 AC 63 ms
22,060 KB
testcase_16 AC 64 ms
21,876 KB
testcase_17 AC 64 ms
21,840 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 List<int> SelectedIndList;
    }

    struct MemoDef
    {
        internal int CurrP;
        internal int CurrSum;
    }

    static void Main()
    {
        List<string> InputList = GetInputList();
        int[] OmoriArr = InputList[1].Split(' ').Select(X => int.Parse(X)).ToArray();
        int UB = OmoriArr.GetUpperBound(0);

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

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

        var MemoList = new List<MemoDef>();

        var stk = new Stack<JyoutaiDef>();
        JyoutaiDef WillPush;
        WillPush.CurrP = 0;
        WillPush.SelectedIndList = new List<int>() { 0 };
        stk.Push(WillPush);

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

            int SelectedSum = DeriveSelectedSum(OmoriArr, Popped.SelectedIndList);

            //クリア判定 
            if (SelectedSum == OmoriSum / 2) {
                //Console.WriteLine("解を発見");
                //Popped.SelectedIndList.ForEach(X => Console.WriteLine(OmoriArr[X]));
                FoundAnswer = true;
                break;
            }

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

            for (int I = Popped.CurrP + 1; I <= UB; I++) {
                //下限値枝切り
                int wkSum = SelectedSum;
                for (int J = I; J <= UB; J++) {
                    wkSum += OmoriArr[J];
                }
                if (wkSum < OmoriSum / 2) break;

                //Push処理
                WillPush.CurrP = I;
                WillPush.SelectedIndList = new List<int>(Popped.SelectedIndList) { I };

                //メモ化探索で再訪禁止
                int NewOmoriSum = DeriveSelectedSum(OmoriArr, WillPush.SelectedIndList);
                if (MemoList.Exists(X => X.CurrP == WillPush.CurrP &&
                    X.CurrSum == NewOmoriSum)) continue;
                MemoList.Add(new MemoDef() { CurrP = I, CurrSum = NewOmoriSum });

                stk.Push(WillPush);
            }
        }
        Console.WriteLine(FoundAnswer ? "possible" : "impossible");
    }

    //添え字のリストを引数として重りの合計を返す
    static int DeriveSelectedSum(int[] pOmoriArr, List<int> pSelectedIndArr)
    {
        int WillReturn = 0;
        pSelectedIndArr.ForEach(X => WillReturn += pOmoriArr[X]);
        return WillReturn;
    }
}
0