SquareMaking
SRM 711 · 2017-02-20 · by Arterm
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
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.
1 1 1 1 Returns: 0
8 8 1 1 Returns: 14
894 197 325 232 Returns: 790
333625 453145 800800 907251 Returns: 921281
412396 690334 841896 843011 Returns: 582177
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.
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.
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.
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