제출 #693018

#제출 시각아이디문제언어결과실행 시간메모리
693018vjudge1Knapsack (NOI18_knapsack)C++17
100 / 100
98 ms35144 KiB
#include <iostream> #include <vector> #include <map> #include <set> #include <stack> #include <queue> #include <algorithm> #include <string> #include <cmath> #include <cstdio> #include <iomanip> #include <fstream> #include <cassert> #include <cstring> #include <unordered_set> #include <unordered_map> #include <numeric> #include <ctime> #include <bitset> #include <complex> #include <chrono> #include <random> #include <functional> #define taskname "D" using namespace std; mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count()); long long Rand(long long l, long long r) { return uniform_int_distribution<long long>(l, r)(rng); } struct perf { chrono::steady_clock::time_point start_; perf() : start_(chrono::steady_clock::now()) {} double elapsed() const { auto stop = chrono::steady_clock::now(); chrono::duration<double> elapsed_seconds = stop - start_; return elapsed_seconds.count(); } }; void solve() { int limit; int type_num; cin >> limit >> type_num; map<int, vector<pair<int, int>>> weights; for (int t = 0; t < type_num; t++) { int value; int weight; int amt; cin >> value >> weight >> amt; if (weight <= limit && amt > 0) { weights[weight].push_back({value, amt}); } } vector<vector<long long>> best( weights.size() + 1, vector<long long>(limit + 1, INT32_MIN)); best[0][0] = 0; int at = 1; for (auto &[w, items] : weights) { sort(items.begin(), items.end(), greater<pair<int, int>>()); for (int i = 0; i <= limit; i++) { best[at][i] = best[at - 1][i]; int copies = 0; int type_at = 0; int curr_used = 0; long long profit = 0; while ((copies + 1) * w <= i && type_at < items.size()) { copies++; profit += items[type_at].first; if (best[at - 1][i - copies * w] != INT32_MIN) { best[at][i] = max( best[at][i], best[at - 1][i - copies * w] + profit); } curr_used++; if (curr_used == items[type_at].second) { curr_used = 0; type_at++; } } } at++; } cout << *max_element(best.back().begin(), best.back().end()) << endl; } signed main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); if (fopen(taskname ".inp", "r")) { freopen(taskname ".inp", "r", stdin); freopen(taskname ".out", "w", stdout); } int t = 1; // cin >> t; while (t--) { solve(); } }

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

knapsack.cpp: In function 'void solve()':
knapsack.cpp:79:53: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   79 |             while ((copies + 1) * w <= i && type_at < items.size())
      |                                             ~~~~~~~~^~~~~~~~~~~~~~
knapsack.cpp: In function 'int main()':
knapsack.cpp:110:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  110 |         freopen(taskname ".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
knapsack.cpp:111:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  111 |         freopen(taskname ".out", "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...