Editorial for Cái túi 2
Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.
Submitting an official solution before solving the problem yourself is a bannable offence.
Lưu ý: Các code mẫu dưới đây chỉ mang tính tham khảo và có thể không AC được bài tập này
Code mẫu của flashmt
#include <iostream> #include <algorithm> #include <cstdio> #include <vector> #define maxM 10010 #define maxW 1010 using namespace std; int m,n,f[maxM],tail[maxW],head[maxW]; vector <int> st[maxW],pos[maxW]; int main() { int w,v,a; cin >> n >> m; while (n--) { cin >> w >> v >> a; for (int i=0;i<w;i++) { st[i].resize(m/w+1); pos[i].resize(m/w+1); head[i]=0; tail[i]=-1; } for (int i=0;i<=m;i++) { int x=i%w, y=i/w, u=f[i]-v*y; while (tail[x]>=head[x] && u>=st[x][tail[x]]) tail[x]--; st[x][++tail[x]]=u; pos[x][tail[x]]=y; while (pos[x][head[x]]+a<y) head[x]++; f[i]=st[x][head[x]]+v*y; } } int re=0; for (int i=0;i<=m;i++) re=max(re,f[i]); cout << re << endl; return 0; }
Code mẫu của ladpro98
#include <bits/stdc++.h> using namespace std; const int N = 10010; int n, m; int W[N], V[N]; int dp[N]; void maximize(int &a, int b) { a = a < b ? b : a; } int main() { ios_base::sync_with_stdio(false); int _n; cin >> _n >> m; for (int i = 1; i <= _n; ++i) { int w, v, c; cin >> w >> v >> c; int sum = 1, j = 1; for (; sum <= c; j += j, sum += j) { ++n; W[n] = w * j; V[n] = v * j; } sum -= j; if (sum < c) { ++n; W[n] = w * (c - sum); V[n] = v * (c - sum); } } for (int i = 1; i <= n; ++i) for (int j = m - W[i]; j >= 0; --j) maximize(dp[j + W[i]], dp[j] + V[i]); int ans = 0; for (int i = 0; i <= m; ++i) maximize(ans, dp[i]); cout << ans << endl; return 0; }
Code mẫu của RR
{$MODE OBJFPC} uses math; const FINP = ''; FOUT = ''; MAXN = 1000; MAXM = 10111; var f1,f2 : text; v,w : array[1..MAXN] of longint; f : array[0..MAXM] of longint; n,m : longint; procedure openF; begin assign(f1,FINP); reset(f1); assign(f2,FOUT); rewrite(f2); end; procedure closeF; begin close(f1); close(f2); end; procedure inp; var i,sl,ww,vv,a,now:longint; begin read(f1,sl,m); for sl:=1 to sl do begin read(f1,ww,vv,a); now:=1; while a>0 do begin if now>a then now:=a; dec(a,now); inc(n); v[n]:=vv*now; w[n]:=ww*now; now:=now shl 1; end; end; end; procedure solve; var i,j:longint; begin for i:=1 to n do for j:=m downto w[i] do f[j]:=max(f[j],f[j-w[i]]+v[i]); j:=0; for i:=1 to m do j:=max(j,f[i]); writeln(f2,j); end; begin openF; inp; solve; closeF; end.
Comments