|
1 | 1 | /* |
2 | | -Problem: A. Spell Check |
3 | | -
|
4 | | -Problem Statement: |
5 | | -Timur allows any permutation of the letters of his name "Timur" as a valid spelling, |
6 | | -but the spelling must contain exactly one uppercase 'T' and the remaining letters |
7 | | -('i', 'm', 'u', 'r') must be lowercase. Given a string s, check if it is a valid spelling |
8 | | -of Timur's name. |
9 | | -
|
10 | | -Approach: |
11 | | -1. If the length of the string is not 5, it cannot be "Timur". |
12 | | -2. Sort the given string. |
13 | | -3. Sort the reference string "Timur". |
14 | | -4. If both sorted strings are equal, then s is a valid permutation of "Timur". |
| 2 | +==================================================== |
| 3 | +PROBLEM STATEMENT: |
| 4 | +==================================================== |
| 5 | +There are n people standing in a circle (n is even). |
| 6 | +Each person looks at the person exactly opposite to them. |
| 7 | +
|
| 8 | +You are given three distinct integers a, b, and c. |
| 9 | +It is known that person 'a' is looking at person 'b'. |
| 10 | +
|
| 11 | +Determine the person that 'c' is looking at. |
| 12 | +If multiple answers exist, print any. |
| 13 | +If no valid answer exists, print -1. |
| 14 | +
|
| 15 | +---------------------------------------------------- |
| 16 | +PROBLEM LINK: |
| 17 | +https://codeforces.com/problemset/problem/1560/B |
| 18 | +==================================================== |
| 19 | +*/ |
15 | 20 |
|
16 | | -Time Complexity: |
17 | | -O(n log n), where n = 5 (effectively constant). |
| 21 | +/* |
| 22 | +==================================================== |
| 23 | +APPROACH: |
| 24 | +==================================================== |
| 25 | +If person 'a' is looking at person 'b', then: |
| 26 | +- The distance between them is |a - b| |
| 27 | +- Since they are opposite, total number of people: |
| 28 | + n = 2 * |a - b| |
| 29 | +
|
| 30 | +Steps: |
| 31 | +1. Compute dist = |a - b| |
| 32 | +2. Compute n = 2 * dist |
| 33 | +3. If a, b, or c is greater than n → invalid → print -1 |
| 34 | +4. Otherwise, the person opposite to c is: |
| 35 | + - c + dist (if within n) |
| 36 | + - else c - dist |
| 37 | +
|
| 38 | +---------------------------------------------------- |
| 39 | +WHY THIS WORKS: |
| 40 | +- In a circular arrangement, opposite positions differ |
| 41 | + by exactly n/2 positions. |
| 42 | +==================================================== |
| 43 | +*/ |
18 | 44 |
|
19 | | -Space Complexity: |
20 | | -O(1), uses only constant extra space. |
| 45 | +/* |
| 46 | +==================================================== |
| 47 | +TIME & SPACE COMPLEXITY: |
| 48 | +==================================================== |
| 49 | +Time Complexity: O(1) per test case |
| 50 | +Space Complexity: O(1) |
| 51 | +==================================================== |
| 52 | +*/ |
21 | 53 |
|
22 | | -Example: |
| 54 | +/* |
| 55 | +==================================================== |
| 56 | +EXAMPLE: |
| 57 | +==================================================== |
23 | 58 | Input: |
24 | 59 | 1 |
25 | | -5 |
26 | | -miurT |
| 60 | +1 3 2 |
27 | 61 |
|
28 | 62 | Output: |
29 | | -YES |
| 63 | +4 |
30 | 64 |
|
31 | | -Question Link: |
32 | | -https://codeforces.com/problemset/problem/1722/A |
| 65 | +Explanation: |
| 66 | +|1 - 3| = 2 → n = 4 |
| 67 | +Opposite of 2 is 2 + 2 = 4 |
| 68 | +==================================================== |
| 69 | +*/ |
33 | 70 |
|
34 | | -Submission Link: |
35 | | -https://codeforces.com/contest/1722/submission/345095137 |
| 71 | +/* |
| 72 | +==================================================== |
| 73 | +SUBMISSION LINK: |
| 74 | +==================================================== |
| 75 | +https://codeforces.com/contest/1560/submission/345095137 |
| 76 | +==================================================== |
36 | 77 | */ |
37 | 78 |
|
38 | | -#include <iostream> |
39 | | -#include <algorithm> |
| 79 | +#include <bits/stdc++.h> |
40 | 80 | using namespace std; |
41 | 81 |
|
42 | | -void spellCheck() { |
43 | | - int n; |
44 | | - cin >> n; |
45 | | - |
46 | | - string s; |
47 | | - cin >> s; |
48 | | - |
49 | | - if (n != 5) { |
50 | | - cout << "NO\n"; |
51 | | - return; |
52 | | - } |
53 | | - |
54 | | - string name = "Timur"; |
55 | | - |
56 | | - sort(s.begin(), s.end()); |
57 | | - sort(name.begin(), name.end()); |
58 | | - |
59 | | - if (s == name) { |
60 | | - cout << "YES\n"; |
61 | | - } else { |
62 | | - cout << "NO\n"; |
63 | | - } |
64 | | -} |
| 82 | +#define fastio() ios::sync_with_stdio(false); cin.tie(nullptr); |
65 | 83 |
|
66 | 84 | int main() { |
| 85 | + fastio(); |
| 86 | + |
67 | 87 | int t; |
68 | 88 | cin >> t; |
69 | 89 |
|
70 | 90 | while (t--) { |
71 | | - spellCheck(); |
| 91 | + long long a, b, c; |
| 92 | + cin >> a >> b >> c; |
| 93 | + |
| 94 | + long long dist = llabs(a - b); |
| 95 | + long long n = 2 * dist; |
| 96 | + |
| 97 | + if (a > n || b > n || c > n) { |
| 98 | + cout << -1 << '\n'; |
| 99 | + continue; |
| 100 | + } |
| 101 | + |
| 102 | + if (c + dist <= n) |
| 103 | + cout << c + dist << '\n'; |
| 104 | + else |
| 105 | + cout << c - dist << '\n'; |
72 | 106 | } |
73 | 107 |
|
74 | 108 | return 0; |
|
0 commit comments