제출 #914298

#제출 시각아이디문제언어결과실행 시간메모리
914298May27_thKnapsack (NOI18_knapsack)C++17
12 / 100
15 ms47860 KiB
#include <bits/stdc++.h> #define int64_t long long #define double long double using namespace std; using type = int64_t; const long long mod = 998244353, inf = 1e18; const int base = 33; const int N = 2e5 + 10; const int LG = 20; int dx[] = {1, -1, 0, 0}; int dy[] = {0, 0, 1, -1}; void Minimize(int64_t &a, int64_t b) {if(b < a) a = b;} void Maximize(int64_t &a, int64_t b) {if(b > a) a = b;} void Add(int64_t& a, int64_t b) {a = a + b; a %= mod;} void Dec(int64_t& a, int64_t b) {a = a - b + mod; a %= mod;} int S, n; vector<pair<int64_t, int64_t>>items[3000]; int64_t f[3000][3000]; void Solve(void) { /** item: v, w, k; sort theo weight: 1 <= w <= S voi moi weight chon toi da S/w thang gia tri nhat f[i][j] la dung den cac thg weight i va dang co space j f[i][j] = f[i - 1][j - w * t] + v * t; **/ cin >> S >> n; for(int i = 1; i <= n; i ++){ int64_t v, w, k; cin >> v >> w >> k; items[w].push_back(make_pair(v, k)); } for(int i = 1; i <= S; i ++){ for(int j = 0; j <= S; j ++){ f[i][j] = -inf; } } f[0][0] = 0; for(int i = 1; i <= S; i ++){ for(int j = 0; j <= S; j ++){ f[i][j] = f[i - 1][j]; } int sz = items[i].size(); if(sz > 0){ sort(items[i].rbegin(), items[i].rend()); for(int j = 0; j <= S; j ++){ int64_t tot = 0; int64_t space = j; for(int z = 1; z <= min(sz, j/i); z ++){ int64_t v = items[i][z - 1].first; int64_t k = items[i][z - 1].second; int t = min(space/i, k); tot = tot + v * t; space = space - t * i; if(space < i) break; } Maximize(f[i][j], f[i - 1][space] + tot); } } } cout << f[S][S]; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); if(fopen("time.in", "r")){ freopen("time.in", "r", stdin); freopen("time.out", "w", stdout); } if(fopen("A.inp", "r")){ freopen("A.inp", "r", stdin); freopen("A.out", "w", stdout); } int tc = 1; //cin >> tc; while(tc --){ Solve(); } }

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

knapsack.cpp: In function 'int main()':
knapsack.cpp:71:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   71 |         freopen("time.in", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
knapsack.cpp:72:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   72 |         freopen("time.out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
knapsack.cpp:75:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   75 |         freopen("A.inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~
knapsack.cpp:76:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   76 |         freopen("A.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...