제출 #990364

#제출 시각아이디문제언어결과실행 시간메모리
990364_rain_Knapsack (NOI18_knapsack)C++14
100 / 100
92 ms6488 KiB
/** author : Knquin_ **/ #include<bits/stdc++.h> using namespace std; using i64 = long long; using ui64 = unsigned long long; #define MASK(x) ((i64)(1) << (x)) #define BIT(mask , x) (((mask) >> (x)) & (1)) #define sz(x) (x).size() #define all(x) (x).begin() , (x).end() mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count()); template<class T> bool maximize(T &a , T b) {if (a < b) return a = b , true; else return false;} template<class T> bool minimize(T &a , T b) {if (a > b) return a = b , true; else return false;} template<class T> T gcd(T x , T y) {while (y) swap(y , x %= y); return x;} template<class T> T lcm(T x , T y) {return (x * y) / gcd(x , y);} template <class T> void compress(vector<T> &a) { sort(a.begin() , a.end()); a.resize(unique(a.begin() , a.end()) - a.begin()); return; } template<class T> void printArr(T& container , string separator = "" , string finish = "\n") { for (auto& item : container) cout << item << separator; cout << finish; } const int maxn = 2e5; const int maxww = 2e3; int maxw , n; int v[maxn + 2] , w[maxn + 2] , k[maxn + 2]; namespace subtask1 { bool condition( ) { return n <= 100; } i64 dp[102][maxww + 2]; void cook(void) { i64 answer = 0; for (int i = 1; i <= n; ++i) { for (int turn = 0; turn <= min(k[i] , maxw / w[i]); ++turn) { for (int used = 0; used <= maxw - w[i] * turn; ++used) maximize(dp[i][used + w[i] * turn] , dp[i - 1][used] + (i64)v[i] * turn); } } for (int i = 0; i <= maxw; ++i) maximize(answer , dp[n][i]); cout << answer << '\n'; } } namespace subtask2 { const int bestsize = 2e3 + 2; i64 dp[maxn + 2]; vector<pair<int , int>> bucket[bestsize]; void cook(void) { for (int i = 1; i <= n; ++i) bucket[w[i]].push_back({v[i] , k[i]}); vector<pair<int , int>> opt; for (int i = 1; i <= maxw; ++i) { sort(all(bucket[i])); int canchoose = maxw / i; for (; canchoose && sz(bucket[i]); --canchoose) { bucket[i].back().second--; opt.push_back({bucket[i].back().first , i}); if (!bucket[i].back().second) bucket[i].pop_back(); } } dp[0] = 0; for(auto& des : opt) { for (int used = maxw - des.second; used >= 0; --used) maximize(dp[used + des.second] , dp[used] + des.first); } cout << *max_element(dp , dp + maxw + 1); } } auto main(void)->int32_t { cin.tie(nullptr)->sync_with_stdio(false); const string name = "main"; if (fopen((name + ".inp").c_str() , "r")) { (void)!freopen((name + ".inp").c_str() , "r" , stdin); (void)!freopen((name + ".out").c_str() , "w+", stdout); } cin >> maxw >> n; for (int i = 1; i <= n; ++i) cin >> v[i] >> w[i] >> k[i]; if (subtask1::condition()) { subtask1::cook(); return 0; } subtask2::cook(); }
#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...