結果
| 問題 |
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 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 37 |
コンパイルメッセージ
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();
}
}
14番