pangfengliu / programmingtasks

programming tasks from my courses
67 stars 17 forks source link

Buckets and Balls #335

Open littlehug opened 6 years ago

littlehug commented 6 years ago

Write a program to put balls into two buckets.

Task Description

We have two buckets A and B and some balls. Each bucket has a weight limit and each ball has a weight. Now we need to put the balls one by one into buckets, and we must make sure that the total weight of balls in a bucket does not exceed the weight limit of that bucket. We will report the the remaining capacity of two buckets after we put each ball into a bucket.

The balls can be put into the bucket according to either of the following two rules.

First fit. We first try to place the ball into A, if it does not fit then we try B. If B still does not fit then we discard the ball. Best fit. We try to put the ball into the bucket that has the smallest remaining capacity after putting the ball. If no bucket can hold this ball then we discard the ball. Note that if both A and B has the same remaining capacity, then we put the ball into A.

Let us illustrate the task with two examples. We are given N, M, and W, where N, M are positive integers indicating the weight limits of A and B respectively, and W is a sequence of positive integer indicating the weight of balls. If N = 5, M = 5, W = {2, 4, 1, 3} and we try first fit, the program should do the following.

Use the same N, M and W. Let’s try best fit, the program should do the following.

Input Format

There are two lines in input file. The first line contains three integers N, M and an integer R indicating the rule. Use first fit when R is 0, and use best fit when R 1. The second line has the weights of the balls.

Output Format

Each line contains two numbers indicating the remaining capacity of A and B after you attempt to put a ball into a bucket.

Subtask

Sample Input 1

10 0 0
1 5 3 4 1

Sample Output 1

9 0
4 0
1 0
1 0
0 0

Sample Input 2

5 5 0
2 4 1 3

Sample Output 2

3 5
3 1
2 1
2 1