#include <bits/stdc++.h>
using namespace std;
struct Range
{
long long int start, end;
};
bool sort_ranges_by_start(const Range& a, const Range& b)
{
return a.start < b.start;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
long long int buildings_cnt, school;
int ranges_cnt;
int i;
cin >> buildings_cnt >> ranges_cnt >> school;
vector<Range> ranges(ranges_cnt);
for (auto& range : ranges)
{
cin >> range.start >> range.end;
}
// Sentinels
ranges.push_back({ buildings_cnt + 1, buildings_cnt + 1 });
ranges.push_back({ 0, 0 });
ranges_cnt += 2;
sort(ranges.begin(), ranges.end(), sort_ranges_by_start);
int school_range_idx = -1;
for (i = 0; i < ranges_cnt; ++i)
{
if (ranges[i].start <= school && ranges[i].end >= school)
{
school_range_idx = i;
break;
}
}
assert(school_range_idx != -1);
long long int min_dist = numeric_limits<long long int>::max();
long long int building;
long long int res = -1;
for (i = school_range_idx + 1; i < ranges_cnt; ++i)
{
building = ranges[i - 1].end + 1;
if (ranges[i].start > building)
{
res = building;
min_dist = building - school;
break;
}
}
for (i = school_range_idx - 1; i >= 0; --i)
{
building = ranges[i + 1].start - 1;
if (ranges[i].end < building)
{
long long int dist = school - building;
if (dist < min_dist)
{
res = building;
}
else if (dist == min_dist)
{
res = min(res, building);
}
break;
}
}
assert(res != -1);
cout << res << "\n";
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 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 | #include <bits/stdc++.h> using namespace std; struct Range { long long int start, end; }; bool sort_ranges_by_start(const Range& a, const Range& b) { return a.start < b.start; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); long long int buildings_cnt, school; int ranges_cnt; int i; cin >> buildings_cnt >> ranges_cnt >> school; vector<Range> ranges(ranges_cnt); for (auto& range : ranges) { cin >> range.start >> range.end; } // Sentinels ranges.push_back({ buildings_cnt + 1, buildings_cnt + 1 }); ranges.push_back({ 0, 0 }); ranges_cnt += 2; sort(ranges.begin(), ranges.end(), sort_ranges_by_start); int school_range_idx = -1; for (i = 0; i < ranges_cnt; ++i) { if (ranges[i].start <= school && ranges[i].end >= school) { school_range_idx = i; break; } } assert(school_range_idx != -1); long long int min_dist = numeric_limits<long long int>::max(); long long int building; long long int res = -1; for (i = school_range_idx + 1; i < ranges_cnt; ++i) { building = ranges[i - 1].end + 1; if (ranges[i].start > building) { res = building; min_dist = building - school; break; } } for (i = school_range_idx - 1; i >= 0; --i) { building = ranges[i + 1].start - 1; if (ranges[i].end < building) { long long int dist = school - building; if (dist < min_dist) { res = building; } else if (dist == min_dist) { res = min(res, building); } break; } } assert(res != -1); cout << res << "\n"; return 0; } |
English