제출 #403166

#제출 시각아이디문제언어결과실행 시간메모리
403166nwatxKnapsack (NOI18_knapsack)C++17
73 / 100
1084 ms2708 KiB
#include <bits/stdc++.h> // see C++ Tips & Tricks
using namespace std;

using ll = long long;

using vi = vector<int>;
#define pb push_back
#define rsz resize
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()

using pi = pair<int,int>;
#define f first
#define s second
#define mp make_pair

void setIO(string name = "") { // name is nonempty for USACO file I/O
	cin.tie(0)->sync_with_stdio(0); // see Fast Input & Output
	if (sz(name)) {
		freopen((name+".in").c_str(), "r", stdin); // see Input & Output
		freopen((name+".out").c_str(), "w", stdout);
	}
}

int S, N;
int dp[2001]; // dp[i] is the max SGD with weight i
int v[100001], w[100001], k[100001];

// Observations:
// there will be at most 2000 of any item.

int main() {
	setIO();

	cin >> S >> N;
	
	for(int i = 0; i < N; i++) {
		cin >> v[i] >> w[i] >> k[i];
		k[i] = min(k[i], 2000);
	}

	// transitions
	// we can include [0, K] elements.

	for(int i = 0; i < N; i++) {
		for(int l = 0; l < k[i]; l++) 
			for(int s = S; s >= 0; s--) {
				if(w[i] + s <= S) dp[w[i] + s] = max(dp[w[i] + s], dp[s] + v[i]);
			}
	}

	cout << dp[S];
}

컴파일 시 표준 에러 (stderr) 메시지

knapsack.cpp: In function 'void setIO(std::string)':
knapsack.cpp:20:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   20 |   freopen((name+".in").c_str(), "r", stdin); // see Input & Output
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
knapsack.cpp:21:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   21 |   freopen((name+".out").c_str(), "w", stdout);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...