Connection Status:
Competition Arena > SquareMaking
SRM 711 · 2017-02-20 · by Arterm · Brute Force, Simple Math
Class Name: SquareMaking
Return Type: int
Method Name: getMinimalPrice
Arg Types: (int, int, int, int)
Problem Statement

Problem Statement

You have four sticks. Their lengths are a, b, c, and d. There is also a stick altering store. For one coin they can change (i.e., either increase or decrease) the length of any stick by 1. You can use the store as many times as you want. You would like to build a square out of your four sticks. Therefore, you need four sticks that each have the same length. Note that the length must be positive.

You are given the ints a, b, c, and d. Compute and return the smallest number of coins you need to spend at the store.

Constraints

  • a will be between 1 and 1,000,000, inclusive.
  • b will be between 1 and 1,000,000, inclusive.
  • c will be between 1 and 1,000,000, inclusive.
  • d will be between 1 and 1,000,000, inclusive.
Examples
0)
1
1
1
1
Returns: 0
1)
8
8
1
1
Returns: 14
2)
894
197
325
232
Returns: 790
3)
333625
453145
800800
907251
Returns: 921281
4)
412396
690334
841896
843011
Returns: 582177
32)
5
6
5
5
Returns: 1

Here the optimal solution is to pay 1 coin to decrease b by 1. After the change you will have four equally long sticks.

33)
4
1
5
4
Returns: 4

Here the optimal side length is 4. Hence, you need to pay the store to increase b three times and to decrease c once. The total amount spent at the store will therefore be 4 coins.

34)
2017
2017
2017
2017
Returns: 0

Here you do not need to change sticks at all.

Submissions are judged against all 49 archived test cases, of which 8 are shown here. Case numbers match the judge’s.

Coding Area

Language: C++17 · define a public class SquareMaking with a public method int getMinimalPrice(int a, int b, int c, int d) · 49 test cases · 2 s / 256 MB per case

Submitting as anonymous