結果
| 問題 |
No.15 カタログショッピング
|
| コンテスト | |
| ユーザー |
14番
|
| 提出日時 | 2016-05-28 20:18:03 |
| 言語 | C#(csc) (csc 3.9.0) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 3,365 bytes |
| コンパイル時間 | 2,841 ms |
| コンパイル使用メモリ | 115,480 KB |
| 実行使用メモリ | 27,584 KB |
| 最終ジャッジ日時 | 2024-10-07 17:33:48 |
| 合計ジャッジ時間 | 7,885 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 5 TLE * 1 -- * 4 |
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc) Copyright (C) Microsoft Corporation. All rights reserved.
ソースコード
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();
}
}
14番