2-09. Packing Problem simulation (20) (zjupat simulation)

Source: Internet
Author: User

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)

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.