제출 #932245

#제출 시각아이디문제언어결과실행 시간메모리
932245RegulusKnapsack (NOI18_knapsack)C++17
100 / 100
39 ms6660 KiB
#include <bits/stdc++.h> #define IO ios::sync_with_stdio(false);cin.tie(0); #define debug(x) cerr << #x << " = " << (x) << ' ' #define bug(x) cerr << (x) << ' ' #define endl cerr << '\n' #define all(v) (v).begin(), (v).end() #define SZ(v) (ll)(v).size() #define lowbit(x) (x)&-(x) #define pb emplace_back #define F first #define S second using namespace std; using ll = long long; using pll = pair<ll, ll>; //#define TEST const int N = 1e5+5; ll wei[N], val[N], num[N], dp[2005], sum[N]; vector<ll> v[2005], com; int main(void) { IO ll n, i, T; cin >> T >> n; for (i=1; i <= n; ++i) cin >> val[i] >> wei[i] >> num[i], v[wei[i]].pb(i); for (i=1; i <= 2000; ++i) { if (v[i].empty()) continue; com.clear(); for (ll x : v[i]) com.pb(val[x]); sort(all(com)); com.resize(unique(all(com))-com.begin()); for (ll j=0; j < SZ(com); ++j) sum[j] = 0; for (ll x : v[i]) { auto it = lower_bound(all(com), val[x]) - com.begin(); sum[it] += num[x]; } ll tot = 0; for (ll t=SZ(com)-1; t >= 0; --t) { tot += sum[t]; if (tot > T/i) sum[t] -= tot - T/i; for (ll j=1; sum[t] > 0; j <<= 1) { ll tmp = min(j, sum[t]); sum[t] -= tmp; for (ll k=T; k >= tmp*i; --k) { dp[k] = max(dp[k], dp[k-tmp*i] + com[t]*tmp); } } if (tot > T/i) break; } } cout << dp[T] << '\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...