제출 #949742

#제출 시각아이디문제언어결과실행 시간메모리
949742Akshat369Knapsack (NOI18_knapsack)C++17
100 / 100
177 ms248396 KiB
#include <bits/stdc++.h> using namespace std; #define int long long #define INF (int)1e18 #define endl '\n' const int mod = 1000 * 1000 * 1000 + 7; const int N = 100005; #define f first #define s second #define rep(i, a, b) for(int i = (a); i < (b); i++) #define rrep(i, a, b) for(int i = (a); i > (b); i--) #define vi vector<int> #define pii pair<int, int> #define all(x) (x).begin(), (x).end() mt19937_64 RNG(chrono::steady_clock::now().time_since_epoch().count()); /* */ void Solve() { int s , n; cin>>s>>n; vector<vector<pair<int,int>> > a(s+1); for (int i = 0; i < n; ++i) { int v , w , k; cin>>v>>w>>k; if (v>0 and w <= s){ a[w].push_back({v,k}); } } vector<pii> item; for (int i = 1; i <=s; ++i) { sort(a[i].begin(), a[i].end()); int lim = s/ i; while (!a[i].empty() and lim > 0){ if (a[i].back().second==0){ //count of elments is zero a[i].pop_back(); continue; } a[i].back().second--; item.push_back({a[i].back().first,i}); lim--; } } //now its normal knapsack problem, where there s log s elment int sz = (int)item.size(); vector<vector<int> > dp(sz, vector<int> (s+1)); // debug(item); for (int i = 0; i < sz; ++i) { for(int j = item[i].second ; j <= s ; j++){ if (i==0){ dp[i][j] = max(dp[i][j] , item[i].first); continue; } dp[i][j] = max(dp[i-1][j-item[i].second] + item[i].first , dp[i-1][j]); } } // debug(dp); cout<<dp[sz-1][s]<<endl; } int32_t main() { auto begin = std::chrono::high_resolution_clock::now(); ios_base::sync_with_stdio(false); cin.tie(0); int t = 1; // cin >> t; for (int i = 1; i <= t; i++) { //cout << "Case #" << i << ": "; Solve(); } auto end = std::chrono::high_resolution_clock::now(); auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin); cerr << "Time measured: " << elapsed.count() * 1e-9 << " seconds.\n"; return 0; }
#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...