Link: http://pat.zju.edu.cn/contests/ds/2-09
Assume there are n items in the size of S1, S2 ,..., Si ,..., SN, where Si is an integer that satisfies 1 <= SI <= 100. These items should be loaded into a batch of boxes with a capacity of 100 (serial number 1 ~ N. The packing method is to scan the box for each item in sequence and put the item into the first box that can hold it. Please write a program to simulate this packing process, and output the serial number of the box where each item is located, and the number of boxes required to place all items.
Input format description:
Enter N (<= 1st) as the number of items in Row 1, and n positive integers Si (1 <= SI <= 1000, indicating the size of item I) in Row 2 ).
Output format description:
Output the size of each item and the number of boxes in the input order. Each item occupies one row and the number of boxes in the last row.
Sample input and output:
Serial number |
Input |
Output |
1 |
860 70 80 90 30 40 10 20 |
60 170 280 390 430 140 510 120 25 |
2 |
6100 90 80 70 60 50 |
100 190 280 370 460 550 66 |
3 |
12 |
2 11 |
The Code is as follows:
#include <cstdio>#include <cstring>const int maxn = 1017;int a[maxn], b[maxn], c[maxn];void init(){ for(int i = 0; i < maxn; i++) b[i] = 100;}void solve(int n){ int maxx = 0; for(int i = 1; i <= n; i++) { for(int j = 1; ; j++) { if(b[j]>=a[i]) { b[j] -= a[i]; if(maxx < j) maxx = j; c[i] = j; break; } } } for(int i = 1; i <= n; i++) { printf("%d %d\n",a[i],c[i]); } printf("%d\n",maxx);}int main(){ int n; while(~scanf("%d",&n)) { init(); for(int i = 1; i <= n; i++) { scanf("%d",&a[i]); } solve(n); } return 0;}
2-09. Packing Problem simulation (20) (zjupat simulation)