제출 #593620

#제출 시각아이디문제언어결과실행 시간메모리
593620wyhong3103Knapsack (NOI18_knapsack)C++14
29 / 100
2 ms340 KiB
#include <bits/stdc++.h> #include <bits/stdc++.h> #define all(x) begin(x),end(x) #define fir first #define sec second #define sz(x) x.size() #define pb push_back using namespace std; using vi = vector<int>; using pi = pair<int,int>; using pdb = pair<double,double>; using ll = long long; using pll = pair<ll,ll>; const double EPS = (1e-7); void setio(string s){ freopen((s + ".in").c_str(),"r",stdin); freopen((s + ".out").c_str(),"w",stdout); } const int MXN = 1e5 + 5; const int MXS = 2005; //{val, k used} pll dp[MXS]; pll dp1[MXS]; struct Item{ int v, w, k; }; void solve(){ int n, s; cin >> s >> n; vector<Item> items(n); for(auto& i : items) cin >> i.v >> i.w >> i.k; for(int i{}; i < n; i++){ for(int j{1}; j <= s; j++){ //based on last if (dp[j].fir > dp1[j].fir){ dp1[j].fir = dp[j].fir; dp1[j].sec = 0; } if (j-items[i].w >= 0 && dp[j-items[i].w].fir + items[i].v > dp1[j].fir){ dp1[j].fir = dp[j-items[i].w].fir + items[i].v; dp1[j].sec = 1; } //based on cur if (j-items[i].w >= 0 && dp1[j-items[i].w].fir + items[i].v > dp1[j].fir){ if (dp1[j-items[i].w].sec + 1 <= items[i].k){ dp1[j].fir = dp1[j-items[i].w].fir + items[i].v; dp1[j].sec = dp1[j-items[i].w].sec + 1; } } } for(int j{1}; j <= s; j++){ dp[j] = dp1[j]; dp1[j] = {}; } } ll res = 0; for(int i{1}; i <= s; i++) res = max(dp[i].fir, res); cout << res; } int main(){ ios_base::sync_with_stdio(false); cin.tie(nullptr); int t = 1; while(t--){ solve(); } return 0; }

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

knapsack.cpp: In function 'void setio(std::string)':
knapsack.cpp:18:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   18 |  freopen((s + ".in").c_str(),"r",stdin);
      |  ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
knapsack.cpp:19:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   19 |  freopen((s + ".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...