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
#include <algorithm>
#include <cstdint>
#include <iostream>
#include <iterator>
#include <list>

using namespace std;

namespace {
constexpr bool DEBUG = false;
} // namespace

auto main() -> int {
    std::ios_base::sync_with_stdio(false);

    // NOLINTBEGIN(readability-identifier-length)
    int64_t n = 0;
    int64_t s = 0;
    int m = 0;
    // NOLINTEND(readability-identifier-length)
    int64_t address = 0;

    list<int64_t> numbers = {};
    cin >> n >> m >> s;
    for (int i = 0; i < 2 * m; i++) {
        cin >> address;
        numbers.push_back(address);
    }
    numbers.sort();
    if constexpr (DEBUG) {
        for (auto number : numbers) {
            cout << number << " ";
        }
        cout << "\n";
    }
    for (auto iter = next(numbers.begin()); iter != numbers.end() && next(iter) != numbers.end();) {
        if (*iter + 1 == *next(iter)) {
            iter = numbers.erase(iter);
            iter = numbers.erase(iter);
        } else {
            iter++;
            iter++;
        }
    }
    if constexpr (DEBUG) {
        for (auto number : numbers) {
            cout << number << " ";
        }
        cout << "\n";
    }
    auto right = next(numbers.begin());
    for (; right != numbers.end() && next(right) != numbers.end(); right++, right++) {
        if (*right >= s) {
            break;
        }
    }
    auto left = prev(right);
    if (*left > s) {
        cout << s;
        return 0;
    }
    if (*left == 1) {
        cout << *right + 1;
        return 0;
    }
    if (right == numbers.end() || *right == n) {
        cout << *left - 1;
        return 0;
    }
    if (*left == *right) {
        cout << *left - 1;
        return 0;
    }
    if (s - *left <= *right - s) {
        cout << *left - 1;
        return 0;
    }
    cout << *right + 1;
    return (0);
}