제출 #651692

#제출 시각아이디문제언어결과실행 시간메모리
651692marcheKnapsack (NOI18_knapsack)C++17
100 / 100
296 ms145284 KiB
#include <bits/stdc++.h> #pragma GCC optimize("O2") using namespace std; using ll = long long; using vi = vector<int>; #define pb push_back #define all(x) begin(x), end(x) #define sz(x) (int) (x).size() using pi = pair<int,int>; #define fr first #define sc second #define mp make_pair void setIO(string name = "") { cin.tie(0)->sync_with_stdio(0); // see /general/fast-io if (sz(name)) { freopen((name + ".in").c_str(), "r", stdin); // see /general/input-output freopen((name + ".out").c_str(), "w", stdout); } } int inf = 1000000; int MOD = 1e9 + 7; int main() { setIO(); int s,n; cin >> s >> n; vector<vector<int>>a(n, vector<int>(3)); for (int i =0 ; i<n; i++){ for (int j = 0; j<3; j++)cin >> a[i][j]; } unordered_map<int,int>weight; function<bool(vector<int>a, vector<int>b)>sr = [&](vector<int>a,vector<int>b){ if (a[1] < b[1])return true; else if (a[1] > b[1])return false; else{ return a[0] > b[0]; } }; sort(a.begin(), a.end(), sr); vector<int>weights; vector<int>vals; for (int i = 0; i<n; i++){ int S = s/a[i][1] + 1; int togo = S - weight[a[i][1]]; if (togo <= 0)continue; int pushed = 0; for (int j = 0; j<min(togo,a[i][2]); j++){ weights.pb(a[i][1]); vals.pb(a[i][0]); pushed++; } weight[a[i][1]]+=pushed; } int dp[weights.size()+1][2001]; memset(dp, 0, sizeof(dp)); for (int i = 1; i<=weights.size(); i++){ int w = weights[i - 1]; int v = vals[i - 1]; for (int j = 1; j<=s; j++){ dp[i][j] = dp[i-1][j]; if (j - w >= 0){ dp[i][j] = max(dp[i][j], dp[i-1][j - w] + v); } } } cout<<dp[weights.size()][s]; return 0; }

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

knapsack.cpp: In function 'int main()':
knapsack.cpp:60:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   60 |     for (int i = 1; i<=weights.size(); i++){
      |                     ~^~~~~~~~~~~~~~~~
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 /general/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...