답안 #1113131

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1113131 2024-11-15T20:19:36 Z buzdi Knapsack (NOI18_knapsack) C++17
100 / 100
58 ms 19448 KB
#include <iostream>
#include <map>
#include <vector>
#include <queue>
#include <cassert>
#include <algorithm>
#define ll long long

using namespace std;

const int INF = 2e9;
const int SMAX = 2000;

int S, n;
vector<pair<int, int>> groups[SMAX + 1];
int dp[SMAX + 1][SMAX + 1];
int answer;

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);

	cin >> S >> n;
	for (int i = 1; i <= n; i++) {
		int v, w, k;
		cin >> v >> w >> k;
		if (w <= S) {
			groups[w].push_back({ v, k });
		}
	}

	for (int i = 0; i <= S; i++) {
		for (int j = 0; j <= S; j++) {
			dp[i][j] = -INF;
		}
	}
	dp[0][0] = 0;

	for (int w = 1; w <= S; w++) {
		for (int j = 0; j <= S; j++) {
			dp[w][j] = dp[w - 1][j];
		}

		sort(groups[w].begin(), groups[w].end(), greater<pair<int, int>>());
		int total_value = 0;
		int total_weight = 0;
		for (int i = 0; i < groups[w].size() && total_weight + w <= S; i++) {
			for (int p = 1; p <= groups[w][i].second && total_weight + w <= S; p++) {
				total_weight += w;
				total_value += groups[w][i].first;
				for (int j = 0; j <= S; j++) {
					if (j - total_weight >= 0 && dp[w - 1][j - total_weight] != -INF) {
						dp[w][j] = max(dp[w][j], dp[w - 1][j - total_weight] + total_value);
					}
				}
			}
		}
	}

	answer = -INF;
	for (int i = 0; i <= S; i++) {
		answer = max(answer, dp[S][i]);
	}
	cout << answer << '\n';

	return 0;
}

Compilation message

knapsack.cpp: In function 'int main()':
knapsack.cpp:48:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   48 |   for (int i = 0; i < groups[w].size() && total_weight + w <= S; i++) {
      |                   ~~^~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 10832 KB Output is correct
2 Correct 3 ms 10832 KB Output is correct
3 Correct 4 ms 10832 KB Output is correct
4 Correct 3 ms 10832 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 7 ms 15952 KB Output is correct
3 Correct 8 ms 15952 KB Output is correct
4 Correct 8 ms 15952 KB Output is correct
5 Correct 7 ms 15952 KB Output is correct
6 Correct 7 ms 16176 KB Output is correct
7 Correct 7 ms 15952 KB Output is correct
8 Correct 13 ms 15952 KB Output is correct
9 Correct 7 ms 15952 KB Output is correct
10 Correct 7 ms 16176 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 7 ms 15952 KB Output is correct
3 Correct 8 ms 15952 KB Output is correct
4 Correct 8 ms 15952 KB Output is correct
5 Correct 7 ms 15952 KB Output is correct
6 Correct 7 ms 16176 KB Output is correct
7 Correct 7 ms 15952 KB Output is correct
8 Correct 13 ms 15952 KB Output is correct
9 Correct 7 ms 15952 KB Output is correct
10 Correct 7 ms 16176 KB Output is correct
11 Correct 1 ms 336 KB Output is correct
12 Correct 9 ms 15952 KB Output is correct
13 Correct 7 ms 15952 KB Output is correct
14 Correct 8 ms 15952 KB Output is correct
15 Correct 7 ms 15932 KB Output is correct
16 Correct 9 ms 15964 KB Output is correct
17 Correct 8 ms 15952 KB Output is correct
18 Correct 8 ms 15952 KB Output is correct
19 Correct 8 ms 15952 KB Output is correct
20 Correct 8 ms 15952 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 10832 KB Output is correct
2 Correct 3 ms 10832 KB Output is correct
3 Correct 4 ms 10832 KB Output is correct
4 Correct 3 ms 10832 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 7 ms 15952 KB Output is correct
7 Correct 8 ms 15952 KB Output is correct
8 Correct 8 ms 15952 KB Output is correct
9 Correct 7 ms 15952 KB Output is correct
10 Correct 7 ms 16176 KB Output is correct
11 Correct 7 ms 15952 KB Output is correct
12 Correct 13 ms 15952 KB Output is correct
13 Correct 7 ms 15952 KB Output is correct
14 Correct 7 ms 16176 KB Output is correct
15 Correct 1 ms 336 KB Output is correct
16 Correct 9 ms 15952 KB Output is correct
17 Correct 7 ms 15952 KB Output is correct
18 Correct 8 ms 15952 KB Output is correct
19 Correct 7 ms 15932 KB Output is correct
20 Correct 9 ms 15964 KB Output is correct
21 Correct 8 ms 15952 KB Output is correct
22 Correct 8 ms 15952 KB Output is correct
23 Correct 8 ms 15952 KB Output is correct
24 Correct 8 ms 15952 KB Output is correct
25 Correct 1 ms 504 KB Output is correct
26 Correct 11 ms 15952 KB Output is correct
27 Correct 7 ms 16148 KB Output is correct
28 Correct 8 ms 15964 KB Output is correct
29 Correct 7 ms 15952 KB Output is correct
30 Correct 9 ms 15952 KB Output is correct
31 Correct 7 ms 15952 KB Output is correct
32 Correct 13 ms 15952 KB Output is correct
33 Correct 8 ms 15952 KB Output is correct
34 Correct 8 ms 16060 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 10832 KB Output is correct
2 Correct 3 ms 10832 KB Output is correct
3 Correct 4 ms 10832 KB Output is correct
4 Correct 3 ms 10832 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 7 ms 15952 KB Output is correct
7 Correct 8 ms 15952 KB Output is correct
8 Correct 8 ms 15952 KB Output is correct
9 Correct 7 ms 15952 KB Output is correct
10 Correct 7 ms 16176 KB Output is correct
11 Correct 7 ms 15952 KB Output is correct
12 Correct 13 ms 15952 KB Output is correct
13 Correct 7 ms 15952 KB Output is correct
14 Correct 7 ms 16176 KB Output is correct
15 Correct 1 ms 336 KB Output is correct
16 Correct 9 ms 15952 KB Output is correct
17 Correct 7 ms 15952 KB Output is correct
18 Correct 8 ms 15952 KB Output is correct
19 Correct 7 ms 15932 KB Output is correct
20 Correct 9 ms 15964 KB Output is correct
21 Correct 8 ms 15952 KB Output is correct
22 Correct 8 ms 15952 KB Output is correct
23 Correct 8 ms 15952 KB Output is correct
24 Correct 8 ms 15952 KB Output is correct
25 Correct 1 ms 504 KB Output is correct
26 Correct 11 ms 15952 KB Output is correct
27 Correct 7 ms 16148 KB Output is correct
28 Correct 8 ms 15964 KB Output is correct
29 Correct 7 ms 15952 KB Output is correct
30 Correct 9 ms 15952 KB Output is correct
31 Correct 7 ms 15952 KB Output is correct
32 Correct 13 ms 15952 KB Output is correct
33 Correct 8 ms 15952 KB Output is correct
34 Correct 8 ms 16060 KB Output is correct
35 Correct 20 ms 1488 KB Output is correct
36 Correct 33 ms 16844 KB Output is correct
37 Correct 41 ms 17984 KB Output is correct
38 Correct 28 ms 18368 KB Output is correct
39 Correct 35 ms 19112 KB Output is correct
40 Correct 58 ms 19448 KB Output is correct
41 Correct 41 ms 18524 KB Output is correct
42 Correct 43 ms 18524 KB Output is correct
43 Correct 53 ms 18528 KB Output is correct
44 Correct 51 ms 18512 KB Output is correct
45 Correct 35 ms 19364 KB Output is correct
46 Correct 31 ms 18896 KB Output is correct
47 Correct 36 ms 19280 KB Output is correct
48 Correct 43 ms 19272 KB Output is correct
49 Correct 30 ms 19024 KB Output is correct
50 Correct 45 ms 19016 KB Output is correct