Stacked box problem

Source: Internet
Author: User
Tags time limit
Stacked box problem
Time limit:10000ms Memory limit:65536k
Total submit:315 accepted:117
Case Time Limit:1000ms
Description
A port has a number of containers, numbered 1 to N, respectively. The size of each box is the same, now to be some of the containers stacked up, container stacking rules are as follows:
1) Only one container can be stacked directly on each container.
2) containers with smaller numbers cannot be placed on larger containers.
3) Each container is given its own weight and weight, and the weight of all containers on top of each container must not exceed the weight of the container.
You are now required to program and select the most numerous containers, so that they can be stacked up to meet the above conditions, that is, the requirement to fold as high as possible.
Input
The first line is a positive integer n, representing a total of n containers (1≤n≤1000).
The following is a total of n rows, two positive integers per line, separated by a space, respectively, each container's own weight and the amount of weight, two numbers are less than or equal to 3000.
Output
Outputs the total number of containers that can be stacked. The run time does not exceed 10 seconds to go.
Sample Input
5
19 15
7 13
5 7
6 8
1 2
Sample Output

4

Var
 a,w:array[1..1000]of longint;
 f:array[0..1000,0..6000]of longint;
 i,j,n,max1: Longint;
Function Max (a,b:longint): Longint;
 begin
  If A>b then exit (a);
  exit (b);
 end;
Begin
 read (n);
&N Bsp;for i:=1 to N does
  READLN (W[i],a[i]);
 f[n,w[n]]:=1;
 for i:=n-1 downto 1 do
 begi N
  f[i]:=f[i+1];
  for j:=0 to A[i] do
   f[i,j+w[i]]:=max (F[i+1,j]+1,f[i,j+w[i]]);
 end;
 max1:=0;
 for i:=0 to 6000 do
  Max1:=max (Max1,f[1,i]);
 write (MAX1);
End.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.