Submission #1124263

#TimeUsernameProblemLanguageResultExecution timeMemory
1124263wrttKnapsack (NOI18_knapsack)C++20
73 / 100
1096 ms2832 KiB
#include "bits/stdc++.h" using namespace std; #define int long long #define pb emplace_back #define mx(a, b) ((a) > (b) ? (a) : (b)) #define mn(a, b) ((a) < (b) ? (a) : (b)) #define graph(n) vector<int> g[(n)+1]; constexpr int mod = 998244353; constexpr int inf = 1e18; void frx(std::string name) { freopen((name + ".in").c_str(), "r", stdin); freopen((name + ".out").c_str(), "w", stdout); } /* template <typename T, size_t N> ostream &operator<<(ostream &os, const T (&arr)[N]) { os << "["; for(size_t i = 0; i < N; ++i) { if(i>0) { os << ", "; } os<<arr[i]; } return os << "]"; } */ void solve() { int s,n; cin>>s>>n; int v[n], w[n], k[n]; for(int i = 0; i < n; ++i) { cin>>v[i]>>w[i]>>k[i]; } if(n==1) { cout<<(mn(s/w[0], k[0])*v[0])<<"\n"; return; } int dp[s+1]; memset(dp,0,sizeof(dp)); for(int i = 0; i < n; ++i) { for(int mod = 0; mod < w[i]; ++mod) { deque<pair<int,int>> q; for(int j = mod, cnt = 0; j<=s; j+=w[i], ++cnt) { if(!q.empty() && q.front().second < cnt-k[i]) { q.pop_front(); } int curr = dp[j] - cnt*v[i]; while(!q.empty() && q.back().first <= curr) { q.pop_back(); } q.emplace_back(curr,cnt); dp[j] = q.front().first+cnt*v[i]; } } } cout<<dp[s]<<"\n"; } signed main() { std::ios_base::sync_with_stdio(false); //std::cin.tie(nullptr); //frx("piggyback"); #ifdef LOCAL freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); auto start = std::chrono::high_resolution_clock::now(); #endif int t = 1; //cin >> t; int curr = 1; while (t--) { //cout<<"Case "<<curr<<"\n"; ++curr; solve(); } #ifdef LOCAL auto end = std::chrono::high_resolution_clock::now(); std::chrono::duration<double> elapsed = end - start; std::cout << elapsed.count() << " seconds\n"; #endif return 0; }

Compilation message (stderr)

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