-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathThree Friends.cpp
71 lines (69 loc) · 2.22 KB
/
Three Friends.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// https://codeforces.com/contest/1272/problem/A
#include <iostream>
#include <cstdlib>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
int q;
long long a, b, c;
cin >> q;
while (q--) {
cin >> a >> b >> c;
vector <long long> temp = {a, b, c};
sort(temp.begin(), temp.end());
if (temp[0] == temp[1] && temp[1] == temp[2]) {
cout << "0\n";
}
else if (temp[0] == temp[1] || temp[1] == temp[2]) {
if (temp[0] == temp[1]) {
temp[0]++; temp[1]++;
if (temp[0] == temp[2]) {
cout << "0\n";
}
else {
temp[2]--;
cout << 2 * (temp[2] - temp[1]) << "\n";
}
}
else {
temp[2]--; temp[1]--;
if (temp[2] == temp[0]) {
cout << "0\n";
}
else {
temp[0]++;
cout << 2 * (temp[2] - temp[0]) << "\n";
}
}
}
else {
if ((temp[0] + 1) == temp[1] && (temp[2] - 1) == temp[1]) {
cout << "0\n";
}
else if ((temp[0] + 1) == temp[1] || (temp[2] - 1) == temp[1]) {
if ((temp[0] + 1) == temp[1]) {
temp[2]--; temp[0]++;
cout << 2 * (temp[2] - temp[1]) << "\n";
}
else {
temp[0]++; temp[2]--;
cout << 2 * (temp[2] - temp[0]) << "\n";
}
}
else {
temp[0]++;
temp[2]--;
int t1, t2, t3, v1, v2;
t1 = abs(temp[2] - temp[1]) + abs(temp[1] - temp[0]) + abs(temp[2] - temp[0]);
v1 = temp[1] + 1;
t2 = abs(temp[2] - v1) + abs(v1 - temp[0]) + abs(temp[2] - temp[0]);
v2 = temp[1] - 1;
t3 = abs(temp[2] - v2) + abs(v2 - temp[0]) + abs(temp[2] - temp[0]);
cout << min(min(t1, t2), t3) << "\n";
}
}
}
return 0;
}