#include using namespace std; // ??LIS????? int LIS(vector& A) { vector dp; for (int x : A) { auto it = lower_bound(dp.begin(), dp.end(), x); if (it == dp.end()) dp.push_back(x); else *it = x; } return dp.size(); } // ?????????? int optimizedSimulatedAnnealing(int n, vector>& range) { // ??????????????? vector A(n); for (int i = 0; i < n; i++) { A[i] = rand() % (range[i].second - range[i].first + 1) + range[i].first; } // ????LIS?? int bestLIS = LIS(A); vector bestA = A; // ??????? double temperature = 100.0; double coolingRate = 0.995; // ?????? while (temperature > 1e-6) { // ???????????????? vector newA = A; int numChanges = rand() % (n / 10); // ??????? // ???????? for (int i = 0; i < numChanges; i++) { int idx = rand() % n; newA[idx] = rand() % (range[idx].second - range[idx].first + 1) + range[idx].first; } // ????LIS?? int newLIS = LIS(newA); // ??????????? if (newLIS > bestLIS) { bestLIS = newLIS; bestA = newA; } else { // ????????????????????? double prob = exp((newLIS - bestLIS) / temperature); if (rand() / double(RAND_MAX) < prob) { bestLIS = newLIS; bestA = newA; } } // ?? temperature *= coolingRate; } return bestLIS; } int main() { int n; cin >> n; // ???? vector> range(n); for (int i = 0; i < n; i++) { cin >> range[i].first >> range[i].second; } // ?????????????? int maxLIS = optimizedSimulatedAnnealing(n, range); cout << maxLIS << endl; return 0; }