# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
467860 | 2021-08-25T02:09:27 Z | rainliofficial | Knapsack (NOI18_knapsack) | C++17 | 2 ms | 388 KB |
#include <bits/stdc++.h> using namespace std; typedef long long ll; struct Item{ int v, w, k; bool operator<(const Item& o) const{ return o.v < v; }; }; const int MAXN = 1e5 + 5, MAXS = 2000 + 5; int maxW, n; map<int, vector<Item>> arr; ll dp[MAXS][MAXS]; int main(){ cin.tie(0)->sync_with_stdio(0); freopen("file.in", "r", stdin); cin >> maxW >> n; for (int i=0; i<n; i++){ int v, w, k; cin >> v >> w >> k; if (arr.find(w) == arr.end()){ arr.insert({w, vector<Item>()}); } arr[w].push_back({v, w, k}); } int weightGroup = 1; for (auto currW : arr){ vector<Item> curr = currW.second; sort(curr.begin(), curr.end()); int id = 0; for (int i=0; i<=maxW; i++){ dp[weightGroup][i] = dp[weightGroup-1][i]; int sum = 0; // Try to use as many of this current weightGroup as possible int copies = 0; int ind = 0; int totCopies = 1; while (ind < curr.size()){ if (totCopies * currW.first > i){ break; } if (copies >= curr[ind].k){ ind++; copies = 0; } if (ind >= curr.size()){ break; } sum += curr[ind].v; dp[weightGroup][i] = max(dp[weightGroup][i], dp[weightGroup-1][i - totCopies * currW.first] + sum); copies++; totCopies++; } } weightGroup++; } /*for (int i=1; i<weightGroup; i++){ for (int j=0; j<=maxW; j++){ cout << dp[i][j] << " "; } cout << "\n"; }*/ ll ans = 0; for (int i=0; i<=maxW; i++){ ans = max(ans, dp[weightGroup-1][i]); } cout << ans << '\n'; }
Compilation message
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Incorrect | 2 ms | 388 KB | Output isn't correct |
2 | Halted | 0 ms | 0 KB | - |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Incorrect | 2 ms | 332 KB | Output isn't correct |
2 | Halted | 0 ms | 0 KB | - |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Incorrect | 2 ms | 332 KB | Output isn't correct |
2 | Halted | 0 ms | 0 KB | - |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Incorrect | 2 ms | 388 KB | Output isn't correct |
2 | Halted | 0 ms | 0 KB | - |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Incorrect | 2 ms | 388 KB | Output isn't correct |
2 | Halted | 0 ms | 0 KB | - |