/****************************************************************************** Online C++ Compiler. Code, Compile, Run and Debug C++ program online. Write your code in this editor and press "Run" button to compile and execute it. *******************************************************************************/ #include <iostream> #include <algorithm> long n, m, s; struct Przedzial{ long x; long y; }; bool porownaj (Przedzial a, Przedzial b) { Przedzial c; if(a.x>b.x) { return false; } else if(a.x==b.x) { c.x=a.x; if(a.y>=b.y) return false; else return true; } else { return true; } } Przedzial tab[1000]; void wypisz() { for(int i=0; i<m; i++) { std::cout << tab[i].x << ", " << tab[i].y << "\n"; } } long znajdzPrzedS() { long wynik = 0; long popY = 0; for(int i=0; i<m; i++) { if(tab[i].x<=s) // przedzial budynkow przed szkola { if(tab[i].x-1 != popY) // jest jakis wolny budynek miedzy 2 poprzednimi przedzialami lub na poczatku { wynik = tab[i].x-1; } } popY = tab[i].y; } return wynik; } long znajdzPoS() { long wynik = 0; long popY = 0; for(int i=0; i<m; i++) { if(tab[i].x >=s && tab[i].y>=s) // przedzial budynkow przed szkola { if(tab[i].x-1 != popY) // jest jakis wolny budynek miedzy 2 poprzednimi przedzialami lub na poczatku { return popY+1; } } popY = tab[i].y; } return popY+1; } long znajdz_min() { long wynik1 = znajdzPrzedS(); long wynik2 = znajdzPoS(); // std::cout << "wynik1: " << wynik1 << " wynik2: " << wynik2 << "\n"; if(wynik1==0) return wynik2; if(wynik2 >n) return wynik1; if(s-wynik1<= wynik2-s) return wynik1; return wynik2; } int main() { std::cin >> n >> m >> s; // n ile budynkow, s - szkola m - opis for(int i=0; i<m; i++) { long x, y; std::cin >> tab[i].x >> tab[i].y; } std::sort(tab, tab+m, porownaj); //std::cout<< "----\n"; // wypisz(); long min = znajdz_min(); std::cout << min; 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 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 | /****************************************************************************** Online C++ Compiler. Code, Compile, Run and Debug C++ program online. Write your code in this editor and press "Run" button to compile and execute it. *******************************************************************************/ #include <iostream> #include <algorithm> long n, m, s; struct Przedzial{ long x; long y; }; bool porownaj (Przedzial a, Przedzial b) { Przedzial c; if(a.x>b.x) { return false; } else if(a.x==b.x) { c.x=a.x; if(a.y>=b.y) return false; else return true; } else { return true; } } Przedzial tab[1000]; void wypisz() { for(int i=0; i<m; i++) { std::cout << tab[i].x << ", " << tab[i].y << "\n"; } } long znajdzPrzedS() { long wynik = 0; long popY = 0; for(int i=0; i<m; i++) { if(tab[i].x<=s) // przedzial budynkow przed szkola { if(tab[i].x-1 != popY) // jest jakis wolny budynek miedzy 2 poprzednimi przedzialami lub na poczatku { wynik = tab[i].x-1; } } popY = tab[i].y; } return wynik; } long znajdzPoS() { long wynik = 0; long popY = 0; for(int i=0; i<m; i++) { if(tab[i].x >=s && tab[i].y>=s) // przedzial budynkow przed szkola { if(tab[i].x-1 != popY) // jest jakis wolny budynek miedzy 2 poprzednimi przedzialami lub na poczatku { return popY+1; } } popY = tab[i].y; } return popY+1; } long znajdz_min() { long wynik1 = znajdzPrzedS(); long wynik2 = znajdzPoS(); // std::cout << "wynik1: " << wynik1 << " wynik2: " << wynik2 << "\n"; if(wynik1==0) return wynik2; if(wynik2 >n) return wynik1; if(s-wynik1<= wynik2-s) return wynik1; return wynik2; } int main() { std::cin >> n >> m >> s; // n ile budynkow, s - szkola m - opis for(int i=0; i<m; i++) { long x, y; std::cin >> tab[i].x >> tab[i].y; } std::sort(tab, tab+m, porownaj); //std::cout<< "----\n"; // wypisz(); long min = znajdz_min(); std::cout << min; return 0; } |