#include <iostream>
#include <unordered_set>
#include <set>
#include <cmath>
using namespace std;
long long n, s;
int m;
unordered_set<long long> occupaied;
set<long long> proposition;
int main() {
cin >> n >> m >> s;
for (int i = 0; i < m; i++)
{
long long start, stop;
cin >> start >> stop;
occupaied.insert(start);
occupaied.insert(stop);
if (start-1 >= 1)
proposition.insert(start-1);
if (stop+1 <= n)
proposition.insert(stop+1);
}
long long best_idx = 0;
long long best_distance = 100000000000000;
for (auto iter = proposition.begin(); iter != proposition.end(); ++iter)
{
long long current_location = *iter;
if (occupaied.find(current_location) != occupaied.end())
continue;
long long distance = abs(current_location - s);
if (distance < best_distance)
{
best_distance = distance;
best_idx = current_location;
}
}
cout << best_idx;
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 | #include <iostream> #include <unordered_set> #include <set> #include <cmath> using namespace std; long long n, s; int m; unordered_set<long long> occupaied; set<long long> proposition; int main() { cin >> n >> m >> s; for (int i = 0; i < m; i++) { long long start, stop; cin >> start >> stop; occupaied.insert(start); occupaied.insert(stop); if (start-1 >= 1) proposition.insert(start-1); if (stop+1 <= n) proposition.insert(stop+1); } long long best_idx = 0; long long best_distance = 100000000000000; for (auto iter = proposition.begin(); iter != proposition.end(); ++iter) { long long current_location = *iter; if (occupaied.find(current_location) != occupaied.end()) continue; long long distance = abs(current_location - s); if (distance < best_distance) { best_distance = distance; best_idx = current_location; } } cout << best_idx; return 0; } |
English