Hướng dẫn giải của Thang máy vũ trụ
Chỉ dùng lời giải này khi không có ý tưởng, và đừng copy-paste code từ lời giải này. Hãy tôn trọng người ra đề và người viết lời giải.
Nộp một lời giải chính thức trước khi tự giải là một hành động có thể bị ban.
Nộp một lời giải chính thức trước khi tự giải là một hành động có thể bị ban.
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
const maxk=40000; maxn=400; var a:array[1..maxn] of longint; h,c:array[1..maxn] of integer; f:array[0..1,0..maxk] of longint; n:integer; re:longint; procedure rf; var i:integer; begin readln(n); for i:=1 to n do readln(h[i],a[i],c[i]); end; procedure sort(l,r:integer); var i,j:integer; x,y:longint; begin i:=l; j:=r; x:=a[(i+j) div 2]; repeat while a[i]<x do i:=i+1; while a[j]>x do j:=j-1; if i<=j then begin y:=a[i]; a[i]:=a[j]; a[j]:=y; y:=h[i]; h[i]:=h[j]; h[j]:=y; y:=c[i]; c[i]:=c[j]; c[j]:=y; i:=i+1; j:=j-1; end; until i>j; if l<j then sort(l,j); if i<r then sort(i,r); end; procedure pr; var i,j,t,k,p:longint; begin sort(1,n); fillchar(f,sizeof(f),0); f[0,0]:=1; for i:=1 to n do begin t:=i mod 2; f[t]:=f[1-t]; for j:=1 to c[i] do begin p:=h[i]*j; for k:=p to a[i] do if f[1-t,k-p]=1 then f[t,k]:=1; end; end; for i:=a[n] downto 0 do if f[t,i]=1 then break; re:=i; end; procedure wf; begin write(re); end; begin rf; pr; wf; end.
Code mẫu của ladpro98
program elevator; uses math; type e=record h,a,c:longint; end; const maxH=40040; maxn=404; oo=123456789; fi=''; var b:array[0..maxn] of e; f:array[0..maxn,0..maxH] of longint; n,res:longint; procedure input; var inp:text; i:longint; begin assign(inp,fi); reset(inp); readln(inp,n); for i:=1 to n do readln(inp,b[i].h,b[i].a,b[i].c); close(inp); end; procedure swap(i,j:longint); var t:e; begin t:=b[i]; b[i]:=b[j]; b[j]:=t; end; procedure sort(l,r:longint); var p:e; i,j:longint; begin if l>=r then exit; i:=l;j:=r; p:=b[random(r-l+1)+l]; repeat while b[i].a<p.a do inc(i); while b[j].a>p.a do dec(j); if i<=j then begin if i<j then swap(i,j); inc(i);dec(j); end; until i>j; sort(l,j);sort(i,r); end; procedure dp; var i,j,k:longint; begin b[0].a:=oo; for i:=1 to n do for j:=1 to b[i].a do begin //f[i,j]:=-oo; for k:=0 to b[i].c do if (j>=k*b[i].h) and (j-k*b[i].h<=b[i-1].a)then f[i,j]:=max(f[i,j],f[i-1,j-k*b[i].h]+k*b[i].h); res:=max(res,f[i,j]); end; end; begin input; sort(1,n); dp; write(res); end.
Code mẫu của RR
{$R+,Q+,S+} PROGRAM ELEVATOR; CONST fi=''; fo=''; max=40000; maxn=400; TYPE rec=record a:longint; h,c:byte; end; VAR n:integer; k:array[1..maxn] of rec; d:array[0..max] of integer; trace:array[0..max] of integer; kq:longint; Procedure ReadInput; Var f:text; i:integer; Begin Assign(f,fi); Reset(f); Readln(f,n); For i:=1 to n do with k[i] do Readln(f,h,a,c); Close(f); End; Procedure QuickSort; Procedure Sort(l,r:integer); Var i,j:integer; x:longint; tg:rec; Begin i:=l; j:=r; x:=k[(l+r) div 2].a; repeat while k[i].a<x do inc(i); while k[j].a>x do dec(j); if i<=j then begin tg:=k[i]; k[i]:=k[j]; k[j]:=tg; inc(i); dec(j); end; until i>j; if i<r then Sort(i,r); if l<j then Sort(l,j); End; Begin Sort(1,n); End; Procedure Solve; Var i,j,l,m:longint; Begin kq:=0; d[0]:=1; For i:=1 to n do For j:=k[i].c downto 1 do For l:=k[i].a-j*k[i].h downto 0 do if (d[l]=1) and (d[l+j*k[i].h]=0) and (trace[l]<>i) then begin m:=l+j*k[i].h; d[m]:=1; trace[m]:=i; if m>kq then kq:=m; end; End; Procedure WriteOutput; Var f:text; Begin Assign(f,fo); Rewrite(f); Writeln(f,kq); Close(f); End; BEGIN ReadInput; QuickSort; Solve; WriteOutput; END.
Code mẫu của ll931110
program ELEVATOR; const input = ''; output = ''; maxn = 400; maxk = 40000; type block = record h,c,a: longint; end; var free: array[0..maxk] of boolean; t: array[1..maxn] of block; n: longint; procedure init; var f: text; i: longint; begin assign(f, input); reset(f); readln(f, n); for i := 1 to n do readln(f, t[i].h, t[i].a, t[i].c); close(f); end; procedure sort; var i,j: longint; tmp: block; begin for i := 1 to n - 1 do for j := i + 1 to n do if t[i].a > t[j].a then begin tmp := t[i]; t[i] := t[j]; t[j] := tmp; end; end; procedure solve; var i,j,k,ss: longint; begin fillchar(free, sizeof(free), false); free[0] := true; for i := 1 to n do for j := t[i].a downto 0 do if free[j] then begin k := 1; ss := j + t[i].h; while (k <= t[i].c) and (ss <= t[i].a) do begin free[ss] := true; ss := ss + t[i].h; inc(k); end; end; end; procedure printresult; var f: text; res: longint; begin res := maxk; while not free[res] do dec(res); assign(f, output); rewrite(f); writeln(f, res); close(f); end; begin init; sort; solve; printresult; end.
Code mẫu của khuc_tuan
import java.io.*; import java.util.*; public class Main { class TT{ public int a, c, h; } int k; TT[] a; void nhap() throws Exception{ BufferedReader kb=new BufferedReader(new InputStreamReader(System.in)); k=Integer.parseInt(kb.readLine()); a=new TT[k]; for(int i=0;i<k;++i){ StringTokenizer st=new StringTokenizer(kb.readLine()); a[i]=new TT(); a[i].h=Integer.parseInt(st.nextToken()); a[i].a=Integer.parseInt(st.nextToken()); a[i].c=Integer.parseInt(st.nextToken()); } } void xuly(){ Arrays.sort(a, new Comparator<TT>(){ public int compare(TT a,TT b){ return a.a-b.a; } }); TreeSet<Integer> f=new TreeSet<Integer>(); TreeSet<Integer> s=new TreeSet<Integer>(); f.add(0); for(int i=0;i<k;++i){ s.clear(); for(int c=0;c<=a[i].c;++c){ for(int x:f) if(x+c*a[i].h<=a[i].a) s.add(x+c*a[i].h); } while(s.size()>100) s.remove(s.first()); f.clear(); while(s.size()>0){ f.add(s.first()); s.remove(s.first()); } } System.out.println(f.last()); } void run() throws Exception{ nhap(); xuly(); } public static void main(String[] args) throws Exception{ new Main().run(); } }
Bình luận
include <bits/stdc++.h>
using namespace std;
define mp make_pair
vector<pair d int f main ios>base::syncwithstdio(0);cin.tie(0);cout.tie(0); freopen("t.inp","r",stdin); int n; cin>>n; int h, a,c; d.pushback(mp(0,mp(0,0))); for(int i=0;i<n;++i) { cin>>h>>a>>c; d.push_back(mp(a,mp(h,c))); } int ans=0; sort(d.begin(), d.end()); for(int i=1;i<=n;++i){ a= d[i].first; h= d[i].second.first; c= d[i].second.second; for(int j=1;j<=a;j++){ f[i][j]=f[i-1][j]; for(int k=1;k<=c;++k){ if(j>=kh && j-kh<=d[i].first){ f[i][j]=max(f[i][j], f[i-1][j-kh]+kh); } } ans=max(ans, f[i][j]); } } cout<<ans; }</pair>