結果

問題 No.527 ナップサック容量問題
ユーザー 14番14番
提出日時 2017-06-10 01:49:45
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 1,162 ms / 2,000 ms
コード長 3,642 bytes
コンパイル時間 1,615 ms
コンパイル使用メモリ 115,132 KB
実行使用メモリ 121,280 KB
最終ジャッジ日時 2023-10-24 04:49:25
合計ジャッジ時間 15,253 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 42 ms
25,888 KB
testcase_01 AC 40 ms
25,900 KB
testcase_02 AC 41 ms
25,896 KB
testcase_03 AC 42 ms
25,900 KB
testcase_04 AC 41 ms
25,788 KB
testcase_05 AC 232 ms
54,448 KB
testcase_06 AC 1,162 ms
119,384 KB
testcase_07 AC 369 ms
60,040 KB
testcase_08 AC 163 ms
44,316 KB
testcase_09 AC 44 ms
25,780 KB
testcase_10 AC 769 ms
99,276 KB
testcase_11 AC 361 ms
72,556 KB
testcase_12 AC 240 ms
59,320 KB
testcase_13 AC 1,109 ms
121,280 KB
testcase_14 AC 115 ms
44,332 KB
testcase_15 AC 214 ms
53,348 KB
testcase_16 AC 40 ms
25,900 KB
testcase_17 AC 196 ms
47,192 KB
testcase_18 AC 262 ms
62,124 KB
testcase_19 AC 40 ms
25,896 KB
testcase_20 AC 89 ms
35,084 KB
testcase_21 AC 687 ms
112,556 KB
testcase_22 AC 403 ms
78,512 KB
testcase_23 AC 611 ms
100,968 KB
testcase_24 AC 51 ms
27,932 KB
testcase_25 AC 294 ms
58,668 KB
testcase_26 AC 237 ms
53,752 KB
testcase_27 AC 269 ms
54,668 KB
testcase_28 AC 241 ms
53,396 KB
testcase_29 AC 759 ms
113,492 KB
testcase_30 AC 40 ms
25,904 KB
testcase_31 AC 42 ms
25,904 KB
testcase_32 AC 43 ms
25,896 KB
testcase_33 AC 43 ms
25,904 KB
testcase_34 AC 43 ms
25,904 KB
testcase_35 AC 322 ms
65,160 KB
testcase_36 AC 40 ms
25,888 KB
testcase_37 AC 144 ms
44,372 KB
testcase_38 AC 200 ms
53,072 KB
testcase_39 AC 210 ms
50,072 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.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();
	}
}
0