#include <bits/stdc++.h> using namespace std; vector <long long> zaj; long long nrwd(long long ls){ ls--; if(ls > 0 && zaj[ls - 1] + 1 != zaj[ls]) return zaj[ls] - 1; else if(ls == 0) return zaj[ls] - 1; else if(ls == -1) return 0; ls--; return nrwd(ls); } long long nrwg(long long ls, long long w){ if(ls < zaj.size() - 1 && zaj[ls + 1] - 1 != zaj[ls]) return zaj[ls] + 1; else if(ls == zaj.size() - 1){ if(zaj[ls] == w) return -1; return zaj[ls] + 1; } ls += 2; return nrwg(ls, w); } long long nr(long long ls, long long c, long long w){ if(ls % 2 == 0) ls++; long long wd = nrwd(ls); long long wg = nrwg(ls, w); if(wd == 0) return wg; else if(wg == -1 || c - wd <= wg - c) return wd; return wg; } int main() { cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(0); long long a, b, c, t1, t2, no = 1000000000003, ls; cin >> a >> b >> c; for(int i = 0; i < b; i++){ cin >> t1 >> t2; zaj.push_back(t1); zaj.push_back(t2); } sort(zaj.begin(), zaj.end()); auto s = lower_bound(zaj.begin(), zaj.end(), c); ls = distance(zaj.begin(), s); cout << nr(ls, c, a); 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 | #include <bits/stdc++.h> using namespace std; vector <long long> zaj; long long nrwd(long long ls){ ls--; if(ls > 0 && zaj[ls - 1] + 1 != zaj[ls]) return zaj[ls] - 1; else if(ls == 0) return zaj[ls] - 1; else if(ls == -1) return 0; ls--; return nrwd(ls); } long long nrwg(long long ls, long long w){ if(ls < zaj.size() - 1 && zaj[ls + 1] - 1 != zaj[ls]) return zaj[ls] + 1; else if(ls == zaj.size() - 1){ if(zaj[ls] == w) return -1; return zaj[ls] + 1; } ls += 2; return nrwg(ls, w); } long long nr(long long ls, long long c, long long w){ if(ls % 2 == 0) ls++; long long wd = nrwd(ls); long long wg = nrwg(ls, w); if(wd == 0) return wg; else if(wg == -1 || c - wd <= wg - c) return wd; return wg; } int main() { cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(0); long long a, b, c, t1, t2, no = 1000000000003, ls; cin >> a >> b >> c; for(int i = 0; i < b; i++){ cin >> t1 >> t2; zaj.push_back(t1); zaj.push_back(t2); } sort(zaj.begin(), zaj.end()); auto s = lower_bound(zaj.begin(), zaj.end(), c); ls = distance(zaj.begin(), s); cout << nr(ls, c, a); return 0; } |