結果

問題 No.527 ナップサック容量問題
ユーザー kuuso1
提出日時 2017-06-09 23:11:37
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 606 ms / 2,000 ms
コード長 1,920 bytes
コンパイル時間 2,369 ms
コンパイル使用メモリ 111,152 KB
実行使用メモリ 29,020 KB
最終ジャッジ日時 2024-09-22 18:07:31
合計ジャッジ時間 7,621 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
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.

ソースコード

diff #
プレゼンテーションモードにする

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
class TEST{
static void Main(){
Sol mySol =new Sol();
mySol.Solve();
}
}
class Sol{
public void Solve(){
int tot = 0;
for(int i=0;i<N;i++) tot += W[i];
int l = 1, r = tot+1;
while(r - l > 1){
int c = (r + l) / 2;
int v = Knap(c);
if(v < V0){
l = c;
} else {
r = c;
}
}
int min = r;
if(Knap(l) == V0) min = l;
l = 1; r = tot+1;
while(r - l > 1){
int c = (r + l) / 2;
int v = Knap(c);
if(v <= V0){
l = c;
} else {
r = c;
}
}
int max = l;
Console.WriteLine(min);
Console.WriteLine(max == tot ? "inf" : max.ToString());
}
int Knap(int weight){
int[] dp = new int[weight+1];
for(int i=1;i<=weight;i++) dp[i] = -1;
dp[0] = 0;
for(int i=0;i<N;i++){
for(int j=weight;j>=0;j--){
if(dp[j] == -1 || j + W[i] > weight) continue;
dp[j + W[i]] = Math.Max(dp[j + W[i]], dp[j] + V[i]);
}
}
int ret = 0;
for(int i=0;i<=weight;i++) ret = Math.Max(ret,dp[i]);
return ret;
}
int N;
int[] V,W;
int V0;
public Sol(){
N = ri();
V = new int[N];
W = new int[N];
for(int i=0;i<N;i++){
var d = ria();
V[i] = d[0]; W[i] = d[1];
}
V0 = ri();
}
static String rs(){return Console.ReadLine();}
static int ri(){return int.Parse(Console.ReadLine());}
static long rl(){return long.Parse(Console.ReadLine());}
static double rd(){return double.Parse(Console.ReadLine());}
static String[] rsa(char sep=' '){return Console.ReadLine().Split(sep);}
static int[] ria(char sep=' '){return Array.ConvertAll(Console.ReadLine().Split(sep),e=>int.Parse(e));}
static long[] rla(char sep=' '){return Array.ConvertAll(Console.ReadLine().Split(sep),e=>long.Parse(e));}
static double[] rda(char sep=' '){return Array.ConvertAll(Console.ReadLine().Split(sep),e=>double.Parse(e));}
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0