結果
問題 | No.527 ナップサック容量問題 |
ユーザー | 14番 |
提出日時 | 2017-06-10 01:49:45 |
言語 | C#(csc) (csc 3.9.0) |
結果 |
AC
|
実行時間 | 1,044 ms / 2,000 ms |
コード長 | 3,642 bytes |
コンパイル時間 | 1,091 ms |
コンパイル使用メモリ | 115,652 KB |
実行使用メモリ | 122,932 KB |
最終ジャッジ日時 | 2024-09-22 21:45:12 |
合計ジャッジ時間 | 11,973 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 39 ms
27,944 KB |
testcase_01 | AC | 40 ms
27,820 KB |
testcase_02 | AC | 40 ms
27,796 KB |
testcase_03 | AC | 40 ms
25,648 KB |
testcase_04 | AC | 38 ms
27,532 KB |
testcase_05 | AC | 208 ms
54,308 KB |
testcase_06 | AC | 988 ms
117,160 KB |
testcase_07 | AC | 326 ms
57,788 KB |
testcase_08 | AC | 142 ms
46,140 KB |
testcase_09 | AC | 37 ms
25,360 KB |
testcase_10 | AC | 685 ms
101,144 KB |
testcase_11 | AC | 355 ms
74,328 KB |
testcase_12 | AC | 238 ms
61,044 KB |
testcase_13 | AC | 1,044 ms
122,932 KB |
testcase_14 | AC | 111 ms
46,216 KB |
testcase_15 | AC | 208 ms
55,216 KB |
testcase_16 | AC | 39 ms
25,520 KB |
testcase_17 | AC | 186 ms
46,760 KB |
testcase_18 | AC | 239 ms
63,948 KB |
testcase_19 | AC | 40 ms
25,756 KB |
testcase_20 | AC | 89 ms
37,172 KB |
testcase_21 | AC | 676 ms
114,476 KB |
testcase_22 | AC | 369 ms
76,048 KB |
testcase_23 | AC | 577 ms
100,784 KB |
testcase_24 | AC | 50 ms
27,932 KB |
testcase_25 | AC | 286 ms
56,444 KB |
testcase_26 | AC | 228 ms
53,304 KB |
testcase_27 | AC | 264 ms
52,300 KB |
testcase_28 | AC | 210 ms
55,280 KB |
testcase_29 | AC | 722 ms
115,364 KB |
testcase_30 | AC | 39 ms
25,744 KB |
testcase_31 | AC | 41 ms
25,488 KB |
testcase_32 | AC | 42 ms
27,816 KB |
testcase_33 | AC | 41 ms
25,624 KB |
testcase_34 | AC | 41 ms
25,504 KB |
testcase_35 | AC | 308 ms
64,808 KB |
testcase_36 | AC | 41 ms
25,748 KB |
testcase_37 | AC | 143 ms
44,180 KB |
testcase_38 | AC | 195 ms
52,780 KB |
testcase_39 | AC | 200 ms
49,620 KB |
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc) Copyright (C) Microsoft Corporation. All rights reserved.
ソースコード
using System; using System.IO; using System.Linq; using System.Collections.Generic; public class Program { public void Proc() { int itemCount = int.Parse(Reader.ReadLine()); for (int i = 0; i < itemCount; i++) { int[] inpt = Reader.ReadLine().Split(' ').Select(a => int.Parse(a)).ToArray(); this.NimList.Add(new Nimotsu(inpt[0], inpt[1])); } this.NimList = this.NimList.OrderBy(a => a.Size).ToList(); this.Target = long.Parse(Reader.ReadLine()); long tmp = this.NimList.Sum(a => a.Size); long min = GetMin(tmp, 1); long max = GetMax(tmp, 1); Console.WriteLine(min); Console.WriteLine(max==long.MaxValue?"inf":max.ToString()); } private long Target; private long GetMax(long max, long min) { if (max - min <= 1) { long tmpMax = GetAns(0, max); long tmpMin = GetAns(0, min); if (Target >= tmpMax) { return long.MaxValue; } if (Target < tmpMin) { return -1; } if (tmpMax == Target) { return max; } return min; } long mid = (max + min) / 2; long midVal = GetAns(0, mid); if (midVal <= Target) { return GetMax(max, mid); } else { return GetMax(mid, min); } } private long GetMin(long max, long min) { if(max-min<=1) { long tmpMax = GetAns(0, max); long tmpMin = GetAns(0, min); if(Target > tmpMax) { return long.MaxValue; } if(Target < tmpMin) { return -1; } if(tmpMin == Target) { return min; } return max; } long mid = (max + min) / 2; long midVal = GetAns(0, mid); if(midVal < Target) { return GetMin(max, mid); } else { return GetMin(mid, min); } } private Dictionary<int, Dictionary<long, long>> dic = new Dictionary<int, Dictionary<long, long>>(); private long GetAns(int idx, long remainSize) { if (remainSize == 0) { return 0; } if (remainSize < 0) { return -1; } if(idx >= this.NimList.Count) { return 0; } if(!dic.ContainsKey(idx)) { dic.Add(idx, new Dictionary<long, long>()); } if(dic[idx].ContainsKey(remainSize)) { return dic[idx][remainSize]; } long ans = 0; if(this.NimList[idx].Size <= remainSize) { long tmp = GetAns(idx + 1, remainSize - this.NimList[idx].Size); if(tmp>=0) { ans = tmp + this.NimList[idx].Value; } tmp = GetAns(idx + 1, remainSize); if(tmp>=0) { ans = Math.Max(ans, tmp); } } dic[idx][remainSize] = ans; return ans; } private List<Nimotsu> NimList = new List<Nimotsu>(); private class Nimotsu { public long Size; public long Value; public Nimotsu(long v, long s) { this.Size = s; this.Value = v; } } public class Reader { private static StringReader sr; public static bool IsDebug = false; public static string ReadLine() { if (IsDebug) { if (sr == null) { sr = new StringReader(InputText.Trim()); } return sr.ReadLine(); } else { return Console.ReadLine(); } } private static string InputText = @" 3 5 3 9 8 3 2 8 "; } public static void Main(string[] args) { #if DEBUG Reader.IsDebug = true; #endif Program prg = new Program(); prg.Proc(); } }