제출 #540205

#제출 시각아이디문제언어결과실행 시간메모리
540205fcwKnapsack (NOI18_knapsack)C++17
100 / 100
210 ms126968 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<vector<pair<int, int>>>g(s+1); 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++) g[w[i]].push_back({v[i], k[i]}); for(int i=1;i<=s;i++) sort(g[i].rbegin(), g[i].rend()); vector<int>nv, nw; for(int i=1;i<=s;i++){ for(int j=0,l=0;j<s/i && l<(int)g[i].size();l++){ for(int c=0;c<g[i][l].nd && j<s/i;c++, j++){ nv.push_back(g[i][l].st); nw.push_back(i); } } } int m = (int)nv.size(); vector<vector<int>>dp(m+1, vector<int>(s+1)); for(int i=0;i<m;i++){ for(int j=0;j<=s;j++){ dp[i+1][j] = max(dp[i+1][j], dp[i][j]); if(j + nw[i] <= s){ dp[i+1][j+nw[i]] = max(dp[i+1][j+nw[i]], dp[i][j] + nv[i]); } } } int ans = 0; for(int j=0;j<=s;j++) ans = max(ans, dp[m][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...