#include <algorithm>
#include <iostream>
#include <list>
#include <vector>
using namespace std;
class Node {
public:
long long start, end;
bool operator<(const Node &other) const {
return start < other.start;
}
};
vector<Node> v;
list<Node*> lst;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long n, s;
int m;
cin >> n >> m >> s;
for (int i = 0; i < m; ++i) {
long long start, end;
cin >> start >> end;
v.push_back(Node(start, end));
}
sort(v.begin(), v.end());
for (Node &node: v) {
lst.push_back(&node);
}
auto it = lst.begin();
while(it != lst.end()) {
long long start1 = (*it)->start;
long long end1 = (*it)->end;
it++;
if(it == lst.end()) {
break;
}
long long start2 = (*it)->start;
if(start2 == end1 + 1) {
(*it)->start = start1;
it--;
it = lst.erase(it);
}
}
long long best = 2000000000000;
long long res;
for (auto it = lst.begin(); it != lst.end(); ++it) {
long long start = (*it)->start;
long long end = (*it)->end;
if(s >= start && s <= end) {
if(start > 1) {
best = s - start + 1;
res = start-1;
}
if(end < n && end - s + 1 < best) {
best = end - s + 1;
res=end+1;
}
break;
}
}
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 | #include <algorithm> #include <iostream> #include <list> #include <vector> using namespace std; class Node { public: long long start, end; bool operator<(const Node &other) const { return start < other.start; } }; vector<Node> v; list<Node*> lst; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long n, s; int m; cin >> n >> m >> s; for (int i = 0; i < m; ++i) { long long start, end; cin >> start >> end; v.push_back(Node(start, end)); } sort(v.begin(), v.end()); for (Node &node: v) { lst.push_back(&node); } auto it = lst.begin(); while(it != lst.end()) { long long start1 = (*it)->start; long long end1 = (*it)->end; it++; if(it == lst.end()) { break; } long long start2 = (*it)->start; if(start2 == end1 + 1) { (*it)->start = start1; it--; it = lst.erase(it); } } long long best = 2000000000000; long long res; for (auto it = lst.begin(); it != lst.end(); ++it) { long long start = (*it)->start; long long end = (*it)->end; if(s >= start && s <= end) { if(start > 1) { best = s - start + 1; res = start-1; } if(end < n && end - s + 1 < best) { best = end - s + 1; res=end+1; } break; } } cout << res << '\n'; return 0; } |
English