#include <iostream>
#include <set>
#include <vector>
using namespace std;
int znajdz_najblizszy_wolny_budynek(int n, int m, int s, vector<pair<int, int>> &przedzialy) {
set<int> zajete;
for (auto &przedzial : przedzialy) {
for (int i = przedzial.first; i <= przedzial.second; i++) {
zajete.insert(i);
}
}
int offset = 0;
while (true) {
if (s - offset >= 1 && zajete.find(s - offset) == zajete.end()) {
return s - offset;
}
if (s + offset <= n && zajete.find(s + offset) == zajete.end()) {
return s + offset;
}
offset++;
}
}
int main() {
int n, m, s;
cin >> n >> m >> s;
vector<pair<int, int>> przedzialy(m);
for (int i = 0; i < m; i++) {
cin >> przedzialy[i].first >> przedzialy[i].second;
}
cout << znajdz_najblizszy_wolny_budynek(n, m, s, przedzialy) << 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 | #include <iostream> #include <set> #include <vector> using namespace std; int znajdz_najblizszy_wolny_budynek(int n, int m, int s, vector<pair<int, int>> &przedzialy) { set<int> zajete; for (auto &przedzial : przedzialy) { for (int i = przedzial.first; i <= przedzial.second; i++) { zajete.insert(i); } } int offset = 0; while (true) { if (s - offset >= 1 && zajete.find(s - offset) == zajete.end()) { return s - offset; } if (s + offset <= n && zajete.find(s + offset) == zajete.end()) { return s + offset; } offset++; } } int main() { int n, m, s; cin >> n >> m >> s; vector<pair<int, int>> przedzialy(m); for (int i = 0; i < m; i++) { cin >> przedzialy[i].first >> przedzialy[i].second; } cout << znajdz_najblizszy_wolny_budynek(n, m, s, przedzialy) << endl; return 0; } |
English