SPOJ - TEAM2 A Famous ICPC Team

Description

Mr. B, Mr. G, Mr. M and their coach Professor S are planning their way to Warsaw for the ACM-ICPC World Finals. Each of the four has a square-shaped suitcase with side length Ai (1 <= i <= 4) respectively. They want to pack their suitcases into a large square box. The heights of the large box as well as the four suitcases are exactly the same. So they only need to consider the large box’s side length. Of course, you should write a program to output the minimum side length of the large box so that the four suitcases can be put into the box without overlapping.

Input

Each test case contains only one line containing 4 integers Ai (1<= i <=4, 1<= Ai <=1,000,000,000) indicating the side length of each suitcase.

Output

For each test case, display a single line containing the case number and the minimum side length of the large box required.

Example

Input:
2 2 2 2
2 2 2 1

Output:
Case 1: 4
Case 2: 4

Explanation

For the first case, all suitcases have size 2x2. So they can perfectly be packed in a 4x4 large box without wasting any space.

For the second case, three suitcases have size 2x2 and the last one is 1x1. No matter how to rotate or move, you could find the side length of the box must be at least 4.

[Topic outline] Mr. B, Mr. G, Mr. M and their coach S are planning to go to Warsaw to participate in the ACM-ICPC World Finals. Each of the four of them has a square suitcase with side lengths A_iAi​(1 <= i <= 4). They want to pack all the suitcases into a big box. The height of the big box and the four suitcases are exactly the same. So they only need to consider the side length of the big box. Write a program to output the minimum side length of the big box so that four suitcases can be put into the box without overlapping. 

【answer】 

As long as the two largest small boxes are placed together (either vertically or horizontally), when the remaining two small boxes are placed, the side length must not exceed the side length of the two largest small boxes (also Regardless of whether it is placed vertically or horizontally).

As for how to find the small boxes with the first and second largest sides, we can use the array input, and the last sort will do.

#include <stdio.h>
#include <algorithm>

using namespace std;

int main()
{
    int a[5];
    int t = 1;
    while (~scanf("%d %d %d %d", &a[0], &a[1], &a[2], &a[3])){
        sort(a, a+4);
        printf("Case %d: %d\n", t++, a[2]+a[3]);
    }
        
    return 0;
}

 

Guess you like

Origin blog.csdn.net/Aibiabcheng/article/details/105480425