Niestety, nie byliśmy w stanie w pełni poprawnie wyświetlić tego pliku, ponieważ nie jest zakodowany w UTF-8.
Możesz pobrać ten plik i spróbować otworzyć go samodzielnie.
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;
const int IDX = 1001;
long n, a, b, s, result, idxResult;
int m;
vector<pair<long, long>> tab;
int comparator(pair<int, int> first, pair<int, int> second) {
return first.first < second.first;
}
int main()
{
cin >> n >> m >> s;
result = 2147483647;
for (int i = 0; i < m; i++) {
cin >> a >> b;
pair<long, long> element = make_pair(a, b);
tab.push_back(element);
}
sort(tab.begin(), tab.end(), [](pair<long, long> a, pair<long, long> b) {
return a.first < b.first;
});
/*for (int i = 0; i < m; i++) {
std::cout << tab[i].first << " " << tab[i].second << endl;
}*/
for (int i = 0; i <= m; i++)
{
if (i == m) { // Sprawdzenie ko�cowego przedzia�u
if (n > tab[i - 1].second) {
long x = tab[i - 1].second + 1;
int tempResult = abs(x - s);
if (tempResult < result) {
result = tempResult;
idxResult = x;
}
tempResult = abs(n - s);
if (tempResult < result) {
result = tempResult;
idxResult = n;
}
}
}
else if (i == 0) { // Sprawdzenie pierwszego wolnego numeru
if (tab[i].first > 1) {
int tempResult = abs(1 - s);
if (tempResult < result) {
result = tempResult;
idxResult = 1;
}
tempResult = abs(tab[i].first-1 - s);
if (tempResult < result) {
result = tempResult;
idxResult = tab[i].first-1;
}
}
}
else { // Sprawdzenie przedzia��w pomi�dzy zaj�tymi fragmentami
long x = tab[i - 1].second + 1;
long y = tab[i].first - 1;
if (y >= x) {
int tempResult = abs(x - s);
if (tempResult < result) {
result = tempResult;
idxResult = x;
}
tempResult = abs(y - s);
if (tempResult < result) {
result = tempResult;
idxResult = y;
}
}
}
}
std::cout << idxResult;
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 <iostream> #include <vector> #include <algorithm> #include <cmath> using namespace std; const int IDX = 1001; long n, a, b, s, result, idxResult; int m; vector<pair<long, long>> tab; int comparator(pair<int, int> first, pair<int, int> second) { return first.first < second.first; } int main() { cin >> n >> m >> s; result = 2147483647; for (int i = 0; i < m; i++) { cin >> a >> b; pair<long, long> element = make_pair(a, b); tab.push_back(element); } sort(tab.begin(), tab.end(), [](pair<long, long> a, pair<long, long> b) { return a.first < b.first; }); /*for (int i = 0; i < m; i++) { std::cout << tab[i].first << " " << tab[i].second << endl; }*/ for (int i = 0; i <= m; i++) { if (i == m) { // Sprawdzenie ko�cowego przedzia�u if (n > tab[i - 1].second) { long x = tab[i - 1].second + 1; int tempResult = abs(x - s); if (tempResult < result) { result = tempResult; idxResult = x; } tempResult = abs(n - s); if (tempResult < result) { result = tempResult; idxResult = n; } } } else if (i == 0) { // Sprawdzenie pierwszego wolnego numeru if (tab[i].first > 1) { int tempResult = abs(1 - s); if (tempResult < result) { result = tempResult; idxResult = 1; } tempResult = abs(tab[i].first-1 - s); if (tempResult < result) { result = tempResult; idxResult = tab[i].first-1; } } } else { // Sprawdzenie przedzia��w pomi�dzy zaj�tymi fragmentami long x = tab[i - 1].second + 1; long y = tab[i].first - 1; if (y >= x) { int tempResult = abs(x - s); if (tempResult < result) { result = tempResult; idxResult = x; } tempResult = abs(y - s); if (tempResult < result) { result = tempResult; idxResult = y; } } } } std::cout << idxResult; return 0; } |
English