-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathap.cpp
More file actions
80 lines (63 loc) · 1.74 KB
/
Copy pathap.cpp
File metadata and controls
80 lines (63 loc) · 1.74 KB
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
72
73
74
75
76
77
78
79
80
/*You are given three integers A, B and C. You may perform the following operation an arbitrary number of times: choose one of the numbers A, B, C and either add 1 to it or subtract 1 from it.
Find the minimum number of operations required to make the sequence A, B, C an arithmetic progression, i.e. a sequence which satisfies B - A = C - B.
Input
The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows.
The first and only line of each test case contains three space-separated integers A, B and C.
Output
For each test case, print a single line containing one integer — the minimum required number of operations.
Constraints
1 ≤ T ≤ 10,000
-109 ≤ A, B, C ≤ 109
Subtasks
Subtask #1 (35 points): -102 ≤ A, B, C ≤ 102
Subtask #2 (65 points): original constraints
Example
Input:
5
-5 0 5
-5 7 6
-10 -100 20
1 -1 1
51 23 10
Output:
0
7
105
2
8
Explanation
Example case 1: No operations are needed because 0-(-5) = 5-0.
Example case 2: We can obtain an arithmetic progression in seven operations by adding 1 to A = -5 and subtracting 1 six times from B = 7.
Example case 3: We should add 1 to B 105 times.
*/
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
int t;
scanf("%d", &t);
while(t--) {
ll a, b, c;
scanf("%lld%lld%lld", &a, &b, &c);
ll avg1, avg2;
avg1 = (a + c) / 2;
if(abs(a + c) % 2 == 1) {
if(a + c < 0) {
avg2 = avg1 - 1;
} else {
avg2 = avg1 + 1;
}
} else {
avg2 = avg1;
}
if(avg1 != avg2) {
if(abs(b - avg1) < abs(b - avg2)) {
printf("%lld\n", abs(b - avg2));
} else {
printf("%lld\n", abs(b - avg1));
}
} else {
printf("%lld\n", abs(b - avg1));
}
}
}