답안 #330021

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
330021 2020-11-23T12:49:37 Z oak Knapsack (NOI18_knapsack) C++14
29 / 100
2 ms 492 KB
#include<bits/stdc++.h>
using namespace std;
pair<long long,int> dp[2][2020];
int main(){
	int n,k,s,i,j;
	scanf("%d %d",&s,&n);
	int val[n],wei[n],num[n];
	for(i=0;i<n;i++){
		scanf("%d %d %d",&val[i],&wei[i],&num[i]);
	}
	for(i=0;i<n;i++){
		for(j=1;j<=s;j++){
			dp[i%2][j] = make_pair(dp[(i+1)%2][j].first,num[i]);
			if(j>=wei[i] && dp[(i+1)%2][j-wei[i]].first + val[i] > dp[i%2][j].first){
				dp[i%2][j] = make_pair(dp[(i+1)%2][j-wei[i]].first + val[i],num[i]-1);
			}
			if(j>=wei[i] && dp[i%2][j-wei[i]].second && dp[i%2][j-wei[i]].first + val[i] > dp[i%2][j].first){
				dp[i%2][j] = make_pair(dp[i%2][j-wei[i]].first + val[i],dp[i%2][j-wei[i]].second - 1);
			}
			if(j>=wei[i] && !dp[i%2][j-wei[i]].second && dp[i%2][j-wei[i]].first > dp[i%2][j].first){
				dp[i%2][j] = dp[i%2][j-wei[i]];
			}
		}
	}
	printf("%lld",dp[(i-1)%2][s].first);
}

Compilation message

knapsack.cpp: In function 'int main()':
knapsack.cpp:5:8: warning: unused variable 'k' [-Wunused-variable]
    5 |  int n,k,s,i,j;
      |        ^
knapsack.cpp:6:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
    6 |  scanf("%d %d",&s,&n);
      |  ~~~~~^~~~~~~~~~~~~~~
knapsack.cpp:9:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
    9 |   scanf("%d %d %d",&val[i],&wei[i],&num[i]);
      |   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 492 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 2 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 2 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Incorrect 2 ms 364 KB Output isn't correct
13 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 492 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 2 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Incorrect 2 ms 364 KB Output isn't correct
17 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 492 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 2 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Incorrect 2 ms 364 KB Output isn't correct
17 Halted 0 ms 0 KB -