Submission #540187

#TimeUsernameProblemLanguageResultExecution timeMemory
540187fcwKnapsack (NOI18_knapsack)C++17
73 / 100
134 ms262144 KiB
#include <bits/stdc++.h> #define st first #define nd second using lint = int64_t; constexpr int mod = int(1e9) + 7; constexpr int inf = 0x3f3f3f3f; constexpr int ninf = 0xcfcfcfcf; constexpr lint linf = 0x3f3f3f3f3f3f3f3f; const long double pi = acosl(-1.0); // Returns -1 if a < b, 0 if a = b and 1 if a > b. int cmp_double(double a, double b = 0, double eps = 1e-9) { return a + eps > b ? b + eps > a ? 0 : 1 : -1; } using namespace std; template<typename T, T (*op)(const T&, const T&)> struct monotonic_queue { vector<T> as, aas; vector<T> bs, bbs; void reserve(int N) { as.reserve(N); aas.reserve(N); bs.reserve(N); bbs.reserve(N); } void reduce() { while (!bs.empty()) { as.push_back(bs.back()); aas.push_back(aas.empty() ? bs.back() : op(bs.back(), aas.back())); bs.pop_back(); bbs.pop_back(); } } T get() { if (as.empty()) reduce(); return (bbs.empty() ? aas.back() : op(aas.back(), bbs.back())); } bool empty() const { return (as.empty() && bs.empty()); } int size() const { return int(as.size()) + int(bs.size()); } T front() { if (as.empty()) reduce(); return as.back(); } void push(const T& val) { bs.push_back(val); bbs.push_back(bbs.empty() ? val : op(bbs.back(), val)); } void pop() { if (as.empty()) reduce(); as.pop_back(); aas.pop_back(); } }; // min/max template<typename T> T mapping_max(const T& a, const T& b) { return max(a, b); } template<typename T> using max_queue = monotonic_queue<T, mapping_max>; int main() { cin.tie(nullptr)->sync_with_stdio(false); //ifstream cin; //cin.open("feast.in"); //ofstream cout; //cout.open("feast.out"); int s, n; cin>>s>>n; vector<int>v(n), w(n), k(n); for(int i=0;i<n;i++) cin>>v[i]>>w[i]>>k[i]; for(int i=0;i<n;i++) k[i] = min(k[i], s); vector<vector<int>>dp(n+1, vector<int>(s+1)); for(int i=0;i<n;i++){ for(int j=0;j<w[i];j++){ max_queue<int>q; for(int l=j,c=0;l<=s;l+=w[i],c++){ q.push(dp[i][l] - c * v[i]); dp[i+1][l] = q.get() + c * v[i]; if(c >= k[i]) q.pop(); } } } int ans = 0; for(int j=0;j<=s;j++) ans = max(ans, dp[n][j]); cout<<ans<<"\n"; } /* [ ]Leu o problema certo??? [ ]Ver se precisa de long long [ ]Viu o limite dos fors (é n? é m?) [ ]Tamanho do vetor, será que é 2e5 em vez de 1e5?? [ ]Testar sample [ ]Testar casos de borda [ ]1LL no 1LL << i [ ]Testar mod (é 1e9+7, mesmo?, será que o mod não ficou negativo?) */
#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...