#include <cstdio> #include <cstdlib> #include <cassert> #include <unordered_map> int main() { long long int n, m, s; scanf("%lld %lld %lld\n", &n, &m, &s); std::unordered_map<long long int, long long int> ranges; // The range that contains the school long long int lrange, rrange; for (long long int i = 0; i < m; i++) { long long int x, y; scanf("%lld %lld\n", &x, &y); ranges.insert({x, y}); ranges.insert({y, x}); if (x <= s && s <= y) { lrange = x; rrange = y; } } // Merge the adjacent ranges to our school range while (ranges.contains(lrange - 1)) { lrange = ranges.at(lrange - 1); } while (ranges.contains(rrange + 1)) { rrange = ranges.at(rrange + 1); } if (rrange == n) { // Rightmost house is occupied, we need to choose the left one printf("%lld\n", lrange - 1); } else if (lrange == 1) { // Leftmost house is occupied, we need to choose the right one printf("%lld\n", rrange + 1); } else if (s - lrange <= rrange - s) { // Leftmost house is closer, or equal in distance to school than the right one printf("%lld\n", lrange - 1); } else { printf("%lld\n", rrange + 1); } 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 | #include <cstdio> #include <cstdlib> #include <cassert> #include <unordered_map> int main() { long long int n, m, s; scanf("%lld %lld %lld\n", &n, &m, &s); std::unordered_map<long long int, long long int> ranges; // The range that contains the school long long int lrange, rrange; for (long long int i = 0; i < m; i++) { long long int x, y; scanf("%lld %lld\n", &x, &y); ranges.insert({x, y}); ranges.insert({y, x}); if (x <= s && s <= y) { lrange = x; rrange = y; } } // Merge the adjacent ranges to our school range while (ranges.contains(lrange - 1)) { lrange = ranges.at(lrange - 1); } while (ranges.contains(rrange + 1)) { rrange = ranges.at(rrange + 1); } if (rrange == n) { // Rightmost house is occupied, we need to choose the left one printf("%lld\n", lrange - 1); } else if (lrange == 1) { // Leftmost house is occupied, we need to choose the right one printf("%lld\n", rrange + 1); } else if (s - lrange <= rrange - s) { // Leftmost house is closer, or equal in distance to school than the right one printf("%lld\n", lrange - 1); } else { printf("%lld\n", rrange + 1); } return 0; } |