結果

問題 No.527 ナップサック容量問題
ユーザー memedosagememedosage
提出日時 2017-06-21 22:47:41
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 41 ms / 2,000 ms
コード長 1,445 bytes
コンパイル時間 782 ms
コンパイル使用メモリ 68,376 KB
実行使用メモリ 41,680 KB
最終ジャッジ日時 2024-04-10 10:03:06
合計ジャッジ時間 2,279 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,816 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 1 ms
6,940 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 8 ms
9,472 KB
testcase_06 AC 33 ms
34,048 KB
testcase_07 AC 22 ms
22,400 KB
testcase_08 AC 7 ms
7,680 KB
testcase_09 AC 2 ms
6,944 KB
testcase_10 AC 32 ms
32,896 KB
testcase_11 AC 14 ms
15,360 KB
testcase_12 AC 9 ms
10,368 KB
testcase_13 AC 36 ms
36,480 KB
testcase_14 AC 7 ms
8,064 KB
testcase_15 AC 13 ms
14,080 KB
testcase_16 AC 2 ms
6,944 KB
testcase_17 AC 8 ms
9,856 KB
testcase_18 AC 11 ms
11,904 KB
testcase_19 AC 2 ms
6,940 KB
testcase_20 AC 6 ms
7,552 KB
testcase_21 AC 39 ms
40,296 KB
testcase_22 AC 22 ms
23,040 KB
testcase_23 AC 34 ms
34,560 KB
testcase_24 AC 4 ms
6,940 KB
testcase_25 AC 18 ms
18,688 KB
testcase_26 AC 15 ms
15,872 KB
testcase_27 AC 16 ms
17,408 KB
testcase_28 AC 14 ms
14,720 KB
testcase_29 AC 41 ms
41,680 KB
testcase_30 AC 1 ms
6,940 KB
testcase_31 AC 2 ms
6,944 KB
testcase_32 AC 2 ms
6,940 KB
testcase_33 AC 2 ms
6,940 KB
testcase_34 AC 2 ms
6,940 KB
testcase_35 AC 14 ms
15,744 KB
testcase_36 AC 1 ms
6,944 KB
testcase_37 AC 9 ms
10,368 KB
testcase_38 AC 12 ms
13,312 KB
testcase_39 AC 10 ms
11,776 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:78:30: warning: ‘max’ may be used uninitialized in this function [-Wmaybe-uninitialized]
   78 |                 std::cout << max << std::endl;
      |                              ^~~
main.cpp:74:22: warning: ‘min’ may be used uninitialized in this function [-Wmaybe-uninitialized]
   74 |         std::cout << min << std::endl;
      |                      ^~~

ソースコード

diff #

#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <utility>

typedef unsigned int uint;

int main() {
	int num_loop;
	std::cin >> num_loop;

	std::vector< std::pair<int, int> > item;
	int maxValue;
	int tmpW = 0;

	for (uint i=0; i<num_loop; ++i) {
		int v, w;
		std::cin >> v;
		std::cin >> w;
		item.emplace_back(v, w);
		tmpW += w;
	}

	std::cin >> maxValue;


	std::vector< std::vector< int > > c(num_loop+1, std::vector< int >(tmpW+1, 0));
	std::vector< std::vector< int > > d(num_loop+1, std::vector< int >(tmpW+1, 0));

	for (uint i=0; i<c.size(); ++i) {
		c[i][0] = 0;
	}
	for (uint j=0; j<c[0].size(); ++j) {
		c[0][j] = 0;
	}

	for (uint i=1; i<c.size(); ++i) {
		for (uint j=1; j<c[i].size(); ++j) {
			if (j < item[i-1].second) {
				c[i][j] = c[i-1][j];
				d[i][j] = 0;
			} else {
				int n = c[i-1][j];
				int y = c[i-1][j-item[i-1].second] + item[i-1].first;
				if (n <= y) {
					c[i][j] = y;
					d[i][j] = 1;
				} else {
					c[i][j] = n;
					d[i][j] = 0;
				}
			}
		}
	}

	int min, max;
	bool flag = false;
	for (uint j=1; j<c[num_loop].size(); ++j) {
		if (c[num_loop][j] == maxValue) {
			if (!flag) {
				min = j;
				flag = true;
			}
		}
		else if (c[num_loop][j] > maxValue) {
			if (flag) {
				max = j-1;
				flag = false;
			}
		}
	}

	std::cout << min << std::endl;
	if (flag) {
		std::cout << "inf" << std::endl;
	} else {
		std::cout << max << std::endl;
	}


	return 0;
}
0