結果

問題 No.617 Nafmo、買い出しに行く
ユーザー reitetsuohreitetsuoh
提出日時 2017-12-27 15:31:31
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 142 ms / 2,000 ms
コード長 992 bytes
コンパイル時間 459 ms
コンパイル使用メモリ 63,052 KB
実行使用メモリ 175,056 KB
最終ジャッジ日時 2023-08-23 07:14:11
合計ジャッジ時間 2,475 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 17 ms
16,560 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 22 ms
21,232 KB
testcase_03 AC 50 ms
50,932 KB
testcase_04 AC 6 ms
6,940 KB
testcase_05 AC 75 ms
86,896 KB
testcase_06 AC 142 ms
175,036 KB
testcase_07 AC 141 ms
175,056 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 106 ms
125,576 KB
testcase_11 AC 122 ms
151,584 KB
testcase_12 AC 73 ms
82,388 KB
testcase_13 AC 53 ms
50,472 KB
testcase_14 AC 62 ms
65,656 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 2 ms
4,376 KB
testcase_17 AC 2 ms
4,380 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 2 ms
4,376 KB
testcase_20 AC 1 ms
4,380 KB
testcase_21 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
using namespace std;
int DynamicPlanMethod(const int MAX_N,const int MAX_W,vector<int> &weight,const vector<int> &value,const int max_weight)
{
	//ダイナミック計画法で解を求める
	// DPテーブル
	// dp[i][j]はi番目以降の品物から重さの和がj以下なるように選んだときの価値の和の最大値を表す。
	vector<vector<int>> dp(MAX_N+1,vector<int>(MAX_W+1));
	for (int j = 0; j <= MAX_W; j++) {
		dp[MAX_N][j] = 0;
	}
	for (int i = MAX_N - 1; i >= 0; i--) {
		for (int j = 0; j <= max_weight; j++) {
			if (j < weight[i]){
				dp[i][j] = dp[i + 1][j];
			}else{
				dp[i][j] = max(dp[i + 1][j], dp[i + 1][j - weight[i]] + value[i]);
			}
		}
	}
	return dp[0][max_weight];
}
int main(int argc, char* argv[])
{
	int N,K;
	cin>>N>>K;
	vector<int> weight(N),value(N,1);
	int i;
	for (i=0;i<N;i++){
		cin>>weight[i];
		value[i]=weight[i];
	}
	int w=DynamicPlanMethod(N,K,weight,value,K);
	cout<<w<<endl;
	return 0;
}
0