結果

問題 No.15 カタログショッピング
ユーザー 14番14番
提出日時 2016-03-31 14:08:38
言語 C#(csc)
(csc 3.9.0)
結果
TLE  
実行時間 -
コード長 3,177 bytes
コンパイル時間 973 ms
コンパイル使用メモリ 113,240 KB
実行使用メモリ 445,696 KB
最終ジャッジ日時 2024-04-10 06:45:45
合計ジャッジ時間 7,872 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 30 ms
24,312 KB
testcase_01 AC 31 ms
24,444 KB
testcase_02 AC 33 ms
24,300 KB
testcase_03 AC 37 ms
25,464 KB
testcase_04 AC 31 ms
24,452 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;
using System.Collections.Generic;
using System.Text;

public class Program
{
    public void Proc() {
        Reader.IsDebug = false;
        int[] inpt = Reader.GetInt();
        int itemCount = inpt[0];
        this.MaxTotalPrice = inpt[1];
        this.PriceList = new int[itemCount + 1];
        List<int> indexList = new List<int>();
        for(int i=1; i<=itemCount; i++) {
            this.PriceList[i] = int.Parse(Reader.ReadLine());
            indexList.Add(i);
        }
        
        List<String> ans = this.GetAns(1, MaxTotalPrice);
        if(ans == null) {
            Console.WriteLine("-1");
        } else
        {
            foreach (string item in ans)
            {
                Console.WriteLine(item.Trim());
            }
        }
    }
    
    private Dictionary<int, Dictionary<int, List<String>>> dic = new Dictionary<int, Dictionary<int, List<string>>>();
    private List<String> GetAns(int idx, int remain) {
        if(!dic.ContainsKey(idx)) {
            dic.Add(idx, new Dictionary<int, List<string>>());
        }
        if(dic[idx].ContainsKey(remain)) {
            return dic[idx][remain];
        }
        if(remain == 0) {
            List<string> ret = new List<string>();
            ret.Add(string.Empty);
            return ret;
        }
        if(remain < 0) {
            return null;
        }
        if(idx >= this.PriceList.Length) {
            return null;
        }
        
        List<string> ans = new List<string>();
        
        List<string> subAns = null;
        if(this.PriceList[idx] <= remain) {
            subAns = this.GetAns(idx + 1, remain - this.PriceList[idx]);
            if(subAns != null) {
                foreach (string item in subAns)
                {
                    ans.Add(idx + " " + item);
                }
            }
        }
        subAns = this.GetAns(idx + 1, remain);
        if(subAns != null) {
            ans.AddRange(subAns);
        }
        if(ans.Count == 0) {
            ans = null;
        }
        this.dic[idx].Add(remain, ans);
        return ans;
    }
    private int MaxTotalPrice = 0;
    
    private int[] PriceList;
    
    
    public class Reader {
        private static String InitText = @"



3 220
180
220
280




        ";
        
        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