結果

問題 No.15 カタログショッピング
ユーザー 14番14番
提出日時 2016-05-28 20:18:03
言語 C#(csc)
(csc 3.9.0)
結果
TLE  
実行時間 -
コード長 3,365 bytes
コンパイル時間 5,085 ms
コンパイル使用メモリ 108,064 KB
実行使用メモリ 383,104 KB
最終ジャッジ日時 2023-07-29 11:50:57
合計ジャッジ時間 10,916 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 69 ms
28,460 KB
testcase_01 AC 70 ms
23,860 KB
testcase_02 AC 70 ms
22,024 KB
testcase_03 AC 74 ms
22,088 KB
testcase_04 AC 69 ms
21,796 KB
testcase_05 TLE -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
権限があれば一括ダウンロードができます
コンパイルメッセージ
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.Text;
using System.Linq;

class Program
{
    public void Proc()
    {
        Reader.IsDebug = false;
        long[] inpt = Reader.ReadLine().Split(' ').Select(a => long.Parse(a)).ToArray();
        int itemCount = (int)inpt[0];
        long goukei = inpt[1];

        this.nedanList = new long[itemCount];

        for (int i = 0; i < itemCount; i++)
        {
            nedanList[i] = long.Parse(Reader.ReadLine());
        }
        List<long> ans = this.GetAns(0, goukei);

        if (ans == null)
        {
            Console.WriteLine("-1");
        }
        else
        {
            ans.ForEach(a => {
                StringBuilder str = new StringBuilder();
                for (int i = 0; i < itemCount; i++)
                {
                    long key = 1 << i;
                    if ((a & key) == key)
                    {
                        if (str.Length > 0)
                        {
                            str.Append(" ");
                        }
                        str.Append(i + 1);
                    }
                }
                Console.WriteLine(str.ToString());
            });
        }

    }

    private Dictionary<int, Dictionary<long, List<long>>> dic = new Dictionary<int, Dictionary<long, List<long>>>();


    private List<long> GetAns(int idx, long remain)
    {
        if (!dic.ContainsKey(idx))
        {
            dic.Add(idx, new Dictionary<long, List<long>>());
        }
        if (dic[idx].ContainsKey(remain))
        {
            return dic[idx][remain];
        }

        List<long> ans = new List<long>();

        if (remain < 0)
        {
            return null;
        }
        if (remain == 0)
        {
            ans.Add(0);
            return ans;
        }
        long key = 1 << idx;
        if (idx == nedanList.Length - 1) {
            if(nedanList[idx] == remain) {
                ans.Add(key);
                return ans;
            } else {
                return null;
            }
        }

        if (this.nedanList[idx] <= remain)
        {
            List<long> ret = this.GetAns(idx + 1, remain - this.nedanList[idx]);
            if (ret != null)
            {
                ret.ForEach(a => ans.Add(a + key));
            }
        }

        List<long> ret2 = this.GetAns(idx + 1, remain);
        if (ret2 != null)
        {
            ans.AddRange(ret2);
        }
        if (ans.Count == 0)
        {
            ans = null;
        }
        dic[idx].Add(remain, ans);
        return ans;
    }

    private long[] nedanList;


    public class Reader
    {
        public static bool IsDebug = true;
        private static String PlainInput = @"
 

12 348940
47250
76500
9520
29960
19520
70960
91390
35610
71190
25840
10150
35000


 
";
        private static System.IO.StringReader Sr = null;
        public static string ReadLine()
        {
            if (IsDebug)
            {
                if (Sr == null)
                {
                    Sr = new System.IO.StringReader(PlainInput.Trim());
                }
                return Sr.ReadLine();
            }
            else
            {
                return Console.ReadLine();
            }
        }

    }
    static void Main()
    {
        Program prg = new Program();
        prg.Proc();
    }
}
0