#include <unistd.h>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <utility>
#include <deque>
#include <iostream>
#include <algorithm>
#include <limits>
// using namespace std;
#define REP(i,n) for(int _n=(n), i=0;i<_n;++i)
#define FOR(i,a,b) for(int i=(a),_b=(b);i<=_b;++i)
#define FORD(i,a,b) for(int i=(a),_b=(b);i>=_b;--i)
#define TRACE(x) cerr << "TRACE(" #x ")" << endl;
#define DEBUG(x) cerr << #x << " = " << (x) << endl;
typedef long long LL;
typedef unsigned long long ULL;
using VINT = std::vector<int>;
using VLL = std::vector<LL>;
using VULL = std::vector<ULL>;
struct Range {
long from = 0;
long to = 0;
};
int main() {
std::ios_base::sync_with_stdio(false);
long n, m, s;
std::cin >> n >> m >> s;
std::map<long, Range> ranges;
long from, to;
REP(i, m) {
std::cin >> from >> to;
ranges.insert({from, {from, to}});
}
Range currentRange;
std::vector<Range> sqashedRanges;
for(auto it = ranges.begin(); it != ranges.end(); it++) {
if (currentRange.from == 0) {
currentRange = it->second;
continue;
}
// if (currentRange.to + 1 == s) {
// currentRange = Range{currentRange.from, currentRange.to + 1};
// }
if (currentRange.to + 1 == it->second.from) {
currentRange = Range{currentRange.from, it->second.to};
continue;
}
sqashedRanges.push_back(currentRange);
currentRange = it->second;
}
if (currentRange.from > 0) {
sqashedRanges.push_back(currentRange);
}
long min = std::numeric_limits<long>::max();
long currentMinLocation = 0;
for (auto& range : sqashedRanges) {
auto beforFrom = range.from -1;
if (beforFrom > 0) {
auto beforFromDistance = std::abs(beforFrom - s);
if (min > beforFromDistance) {
min = beforFromDistance;
currentMinLocation = beforFrom;
}
}
auto afterTo = range.to + 1;
if (afterTo <= n) {
auto afterToDistance = std::abs(afterTo - s);
if (min > afterToDistance) {
min = afterToDistance;
currentMinLocation = afterTo;
}
}
}
std::cout << currentMinLocation << std::endl;
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 88 89 90 | #include <unistd.h> #include <string> #include <vector> #include <map> #include <set> #include <utility> #include <deque> #include <iostream> #include <algorithm> #include <limits> // using namespace std; #define REP(i,n) for(int _n=(n), i=0;i<_n;++i) #define FOR(i,a,b) for(int i=(a),_b=(b);i<=_b;++i) #define FORD(i,a,b) for(int i=(a),_b=(b);i>=_b;--i) #define TRACE(x) cerr << "TRACE(" #x ")" << endl; #define DEBUG(x) cerr << #x << " = " << (x) << endl; typedef long long LL; typedef unsigned long long ULL; using VINT = std::vector<int>; using VLL = std::vector<LL>; using VULL = std::vector<ULL>; struct Range { long from = 0; long to = 0; }; int main() { std::ios_base::sync_with_stdio(false); long n, m, s; std::cin >> n >> m >> s; std::map<long, Range> ranges; long from, to; REP(i, m) { std::cin >> from >> to; ranges.insert({from, {from, to}}); } Range currentRange; std::vector<Range> sqashedRanges; for(auto it = ranges.begin(); it != ranges.end(); it++) { if (currentRange.from == 0) { currentRange = it->second; continue; } // if (currentRange.to + 1 == s) { // currentRange = Range{currentRange.from, currentRange.to + 1}; // } if (currentRange.to + 1 == it->second.from) { currentRange = Range{currentRange.from, it->second.to}; continue; } sqashedRanges.push_back(currentRange); currentRange = it->second; } if (currentRange.from > 0) { sqashedRanges.push_back(currentRange); } long min = std::numeric_limits<long>::max(); long currentMinLocation = 0; for (auto& range : sqashedRanges) { auto beforFrom = range.from -1; if (beforFrom > 0) { auto beforFromDistance = std::abs(beforFrom - s); if (min > beforFromDistance) { min = beforFromDistance; currentMinLocation = beforFrom; } } auto afterTo = range.to + 1; if (afterTo <= n) { auto afterToDistance = std::abs(afterTo - s); if (min > afterToDistance) { min = afterToDistance; currentMinLocation = afterTo; } } } std::cout << currentMinLocation << std::endl; return 0; } |
English