#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
long long n, m, s;
cin >> n >> m >> s;
vector<pair<long long, long long>> occupied(m);
for (long long i = 0; i < m; i++) {
cin >> occupied[i].first >> occupied[i].second;
}
sort(occupied.begin(), occupied.end());
long long left_candidate = s - 1;
long long right_candidate = s + 1;
// Szukamy najbliższego wolnego budynku, idąc od szkoły
while (true) {
bool found_left = true, found_right = true;
// Sprawdzanie, czy left_candidate jest zajęty
for (auto &range : occupied) {
if (left_candidate >= range.first && left_candidate <= range.second) {
found_left = false;
break;
}
}
// Sprawdzanie, czy right_candidate jest zajęty
for (auto &range : occupied) {
if (right_candidate >= range.first && right_candidate <= range.second) {
found_right = false;
break;
}
}
// Jeśli znaleziono wolny budynek po lewej, wybieramy go
if (found_left && left_candidate >= 1) {
cout << left_candidate << endl;
return 0;
}
// Jeśli znaleziono wolny budynek po prawej, wybieramy go
if (found_right && right_candidate <= n) {
cout << right_candidate << endl;
return 0;
}
// Przechodzimy dalej
left_candidate--;
right_candidate++;
}
return 0;
}
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 | #include <iostream> #include <vector> #include <algorithm> using namespace std; int main() { long long n, m, s; cin >> n >> m >> s; vector<pair<long long, long long>> occupied(m); for (long long i = 0; i < m; i++) { cin >> occupied[i].first >> occupied[i].second; } sort(occupied.begin(), occupied.end()); long long left_candidate = s - 1; long long right_candidate = s + 1; // Szukamy najbliższego wolnego budynku, idąc od szkoły while (true) { bool found_left = true, found_right = true; // Sprawdzanie, czy left_candidate jest zajęty for (auto &range : occupied) { if (left_candidate >= range.first && left_candidate <= range.second) { found_left = false; break; } } // Sprawdzanie, czy right_candidate jest zajęty for (auto &range : occupied) { if (right_candidate >= range.first && right_candidate <= range.second) { found_right = false; break; } } // Jeśli znaleziono wolny budynek po lewej, wybieramy go if (found_left && left_candidate >= 1) { cout << left_candidate << endl; return 0; } // Jeśli znaleziono wolny budynek po prawej, wybieramy go if (found_right && right_candidate <= n) { cout << right_candidate << endl; return 0; } // Przechodzimy dalej left_candidate--; right_candidate++; } return 0; } |
English