結果

問題 No.4 おもりと天秤
ユーザー 14番14番
提出日時 2016-03-28 15:52:23
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 541 ms / 5,000 ms
コード長 2,531 bytes
コンパイル時間 2,234 ms
コンパイル使用メモリ 108,660 KB
実行使用メモリ 25,900 KB
最終ジャッジ日時 2023-09-08 16:37:35
合計ジャッジ時間 5,195 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 63 ms
20,852 KB
testcase_01 AC 60 ms
20,696 KB
testcase_02 AC 64 ms
22,764 KB
testcase_03 AC 64 ms
20,680 KB
testcase_04 AC 63 ms
20,720 KB
testcase_05 AC 63 ms
20,712 KB
testcase_06 AC 62 ms
20,828 KB
testcase_07 AC 63 ms
22,712 KB
testcase_08 AC 60 ms
20,840 KB
testcase_09 AC 66 ms
22,740 KB
testcase_10 AC 64 ms
20,804 KB
testcase_11 AC 63 ms
20,760 KB
testcase_12 AC 64 ms
22,828 KB
testcase_13 AC 63 ms
20,848 KB
testcase_14 AC 63 ms
18,608 KB
testcase_15 AC 63 ms
20,792 KB
testcase_16 AC 63 ms
20,804 KB
testcase_17 AC 62 ms
20,760 KB
testcase_18 AC 541 ms
25,900 KB
testcase_19 AC 63 ms
22,836 KB
testcase_20 AC 64 ms
22,760 KB
testcase_21 AC 64 ms
20,628 KB
testcase_22 AC 63 ms
20,708 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
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.Text;
using System.Collections.Generic;

public class Program
{
    public void Proc() {
        Reader.IsDebug = false;
        int omoriCount = int.Parse(Reader.ReadLine());
        
        this.OmoList.AddRange(Reader.GetInt());
        int total = 0;
        foreach (int num in this.OmoList)
        {
            total += num;
        }
        this.OmoList.Sort();
        this.OmoList.Reverse();
        bool ans = (total % 2 == 0);
        if(ans) {
            ans = CanBalance(0, total / 2);
        }
        Console.WriteLine(ans?"possible":"impossible");
    }
    
    private List<int> OmoList = new List<int>();
    
    Dictionary<int,Dictionary<int, bool>> dic = new Dictionary<int,Dictionary<int, bool>>();
    private bool CanBalance(int idx, int remain) {
        if(!dic.ContainsKey(idx)) {
            dic.Add(idx, new Dictionary<int, bool>());
        }
        if(dic[idx].ContainsKey(remain)) {
            return dic[idx][remain];
        }
        if(remain == 0) {
            return true;
        }
        if(remain < 0) {
            return false;
        }
        if (idx >= this.OmoList.Count) {
            return false;
        }
        bool ans = false;
        for (int i=idx; i<this.OmoList.Count; i++) {
            if(this.OmoList[i] <= remain) {
                ans = ans || this.CanBalance(i + 1, remain - this.OmoList[i]);
                if(ans) {
                    break;
                }
            }
        }
        dic[idx].Add(remain, ans);
        return ans;
    }

    public class Reader {
        private static String InitText = @"




7
20 47 73 69 26 62 89




        ";
        
        private static System.IO.StringReader sr = null;
        
        public static bool IsDebug = true;
        
        public static string ReadLine() {
            if(IsDebug) {
                if(sr == null) {
                    sr = new System.IO.StringReader(InitText.Trim());
                }
                return sr.ReadLine();
            } else {
                return Console.ReadLine();
            }
        }
        public static int[] GetInt(char delimiter = ' ') {
            string[] inpt = ReadLine().Split(delimiter);
            int[] ret = new int[inpt.Length];
            for(int i=0; i<inpt.Length; i++) {
                ret[i] = int.Parse(inpt[i]);
            }
            return ret;
        }
    }
    public static void Main(string[] args)
    {
        Program prg = new Program();
        prg.Proc();
    }
}
0