結果

問題 No.4 おもりと天秤
ユーザー hogekihogeki
提出日時 2016-07-31 13:52:22
言語 C#(csc)
(csc 3.9.0)
結果
TLE  
実行時間 -
コード長 1,847 bytes
コンパイル時間 858 ms
コンパイル使用メモリ 104,960 KB
実行使用メモリ 21,888 KB
最終ジャッジ日時 2024-04-24 13:18:10
合計ジャッジ時間 8,164 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 22 ms
17,792 KB
testcase_01 AC 21 ms
17,792 KB
testcase_02 AC 23 ms
17,664 KB
testcase_03 AC 24 ms
17,792 KB
testcase_04 AC 23 ms
17,792 KB
testcase_05 AC 22 ms
17,920 KB
testcase_06 AC 23 ms
17,792 KB
testcase_07 AC 22 ms
17,792 KB
testcase_08 AC 20 ms
17,664 KB
testcase_09 AC 22 ms
17,792 KB
testcase_10 AC 22 ms
17,664 KB
testcase_11 AC 22 ms
17,664 KB
testcase_12 AC 22 ms
17,664 KB
testcase_13 AC 23 ms
17,792 KB
testcase_14 AC 24 ms
17,920 KB
testcase_15 AC 24 ms
17,920 KB
testcase_16 AC 23 ms
17,792 KB
testcase_17 AC 22 ms
17,792 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;

namespace OmoriToTenbin3
{
    class Program
    {
        static int N;
        static int[] W;
        static int[] remainSum;
        static int totalHalf;

        static void Main(string[] args)
        {
            N = int.Parse(Console.ReadLine());
            W = new int[N];
            remainSum = new int[N];
            string[] vals = Console.ReadLine().Split(' ');
            int total = 0;
            for (int i = 0; i < N; i++)
            {
                W[i] = int.Parse(vals[i]);
                total += W[i];
            }

            if(total % 2 != 0)
            {
                Console.WriteLine("impossible");
                return;
            }

            totalHalf = total / 2;

            Array.Sort(W);
            Array.Reverse(W);
            remainSum[N-1] = W[N-1];
            for(int i=N-2; i >= 0; i--)
            {
                remainSum[i] = remainSum[i + 1] + W[i];
            }

            if(Search(0, 0))
            {
                Console.WriteLine("possible");
            }
            else
            {
                Console.WriteLine("impossible");
            }

        }

        static bool Search(int weight, int i)
        {
            if (i >= N)
            {
                return false;
            }


            if (weight == totalHalf)
            {
                return true;
            }
            else if(weight > totalHalf)
            {
                return false;
            }
            else if(weight + remainSum[i] < totalHalf)
            {
                return false;
            }

            if(Search(weight+W[i], i+1))
            {
                return true;
            }
            if(Search(weight, i+1))
            {
                return true;
            }

            return false;

        }

    }
}
0