結果

問題 No.4 おもりと天秤
ユーザー hogeki
提出日時 2016-07-30 21:15:27
言語 C#(csc)
(csc 3.9.0)
結果
WA  
実行時間 -
コード長 2,034 bytes
コンパイル時間 921 ms
コンパイル使用メモリ 111,592 KB
実行使用メモリ 28,184 KB
最終ジャッジ日時 2024-11-06 21:22:28
合計ジャッジ時間 2,388 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 20 WA * 3
権限があれば一括ダウンロードができます
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc)
Copyright (C) Microsoft Corporation. All rights reserved.

ソースコード

diff #

using System;

namespace OmoriToTenbin2
{
    class Program
    {
        static int N;
        static int[] weightCount;
        static int[] remainSum;
        static void Main(string[] args)
        {
            N = int.Parse(Console.ReadLine());
            string[] w = Console.ReadLine().Split(' ');
            weightCount = new int[101];
            remainSum = new int[101];

            for(int i = 0; i < N; i++)
            {
                int j = int.Parse(w[i]);
                weightCount[j] = ~weightCount[j] & 0x01;     
            }

            //for (int i = 1; i <= 100; i++)
            //    Console.WriteLine(weightCount[i]);

            remainSum[1] = weightCount[1];
            for(int i=2; i <= 100; i++)
            {
                remainSum[i] = remainSum[i - 1] + i * weightCount[i];
            } 

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

        }

        static bool Search(int weight, int w)
        {
            if(w == 1)
            {
                if(weightCount[1] == 0)
                {
                    if (weight == 0)
                    {
                        return true;
                    }
                }
                else
                {
                    if(weight+1 == 0 || weight -1 == 0)
                    {
                        return true;
                    }
                }
                return false;
            }

            if(Math.Abs(weight) > remainSum[w])
            {
                return false;
            }

            if(weightCount[w] == 0)
            {
                return Search(weight, w - 1);
            }

            if(Search(weight-w, w-1))
            {
                return true;
            }
            if (Search(weight + w, w - 1))
            {
                return true;
            }
            return false;
        }

    }
}
0