結果

問題 No.4 おもりと天秤
ユーザー hogekihogeki
提出日時 2016-07-30 20:21:59
言語 C#(csc)
(csc 3.9.0)
結果
TLE  
実行時間 -
コード長 1,506 bytes
コンパイル時間 826 ms
コンパイル使用メモリ 104,448 KB
実行使用メモリ 28,304 KB
最終ジャッジ日時 2024-04-24 13:13:30
合計ジャッジ時間 7,759 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 24 ms
28,304 KB
testcase_01 AC 24 ms
17,920 KB
testcase_02 AC 24 ms
18,048 KB
testcase_03 AC 24 ms
17,792 KB
testcase_04 AC 24 ms
17,920 KB
testcase_05 AC 27 ms
18,048 KB
testcase_06 AC 23 ms
18,176 KB
testcase_07 AC 25 ms
18,048 KB
testcase_08 TLE -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
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 OmoriToTenbin
{
    class Program
    {
        static int N;
        static int[] W;
        static int[] remainSum;

        static void Main(string[] args)
        {
            N = int.Parse(Console.ReadLine());
            W = new int[N];
            remainSum = new int[N];
            string[] vals = Console.ReadLine().Split(' ');

            for(int i=0; i < N; i++)
            {
                W[i] = int.Parse(vals[i]);
            }

            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 n)
        {
            if (n == N - 1)
            {
                if (weight + W[n] == 0 || weight - W[n] == 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }

            if (Math.Abs(weight) > remainSum[n])
                return false;

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

    }
}
0