Competition/codeforces
Codeforces Round #552 (Div. 3) B. Make Them Equal
shyram
2019. 4. 17. 13:18
B. Make Them Equal
https://codeforces.com/contest/1154/problem/B
숫자는 총 3 종류 이하가 나와야 한다. 만약 4종류 이상이 나오면 -1을 출력하자.
숫자를 입력 받았으면 다음과 같은 유형으로 나뉜다.
한 종류 : 0을 출력한다
두 종류 : 두 수의 차이가 짝수면 (a+b)/2를 아니면 (a+b)를 출력하자.
세 종류 : 크기순(a < b < c)으로 정렬하여 b-a와 c-b가 같으면 그 값을 출력. 아니라면 -1을 출력하자.
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 | #include <bits/stdc++.h> using namespace std; int arr[3]; int n, m; int main() { ios::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); cin >> n; bool c = true; for (int i = 0; i < n; i++) { cin >> m; bool ch = true; for (int j = 0; j < 3; j++) { if (arr[j] == 0) { arr[j] = m; ch = false; break; } else if (arr[j] == m) { ch = false; break; } } if (ch) { c = false; for (int j = 0; j < 3; j++) { if (arr[j] == m) { c = true; } } } } if (!c) { cout << -1; exit(0); } sort(arr, arr + 3); if (arr[0] == 0) { if (arr[1] == 0) cout << 0; else { if ((arr[2] - arr[1]) % 2 == 0) cout << (arr[2] - arr[1]) / 2; else cout << arr[2] - arr[1]; } } else { if (arr[2] - arr[1] == arr[1] - arr[0]) cout << arr[2] - arr[1]; else cout << -1; } return 0; } | cs |