There are nn rectangles in a row. You can either turn each rectangle by 9090 degrees or leave it as it is. If you turn a rectangle, its width will be height, and its height will be width. Notice that you can turn any number of rectangles, you also can turn all or none of them. You can not change the order of the rectangles.
Find out if there is a way to make the rectangles go in order of non-ascending height. In other words, after all the turns, a height of every rectangle has to be not greater than the height of the previous rectangle (if it is such).
Input
The first line contains a single integer nn (1 \leq n \leq 10^51≤n≤105) — the number of rectangles.
Each of the next nn lines contains two integers w_iwi and h_ihi (1 \leq w_i, h_i \leq 10^91≤wi,hi≤109) — the width and the height of the ii-th rectangle.
Output
Print "YES" (without quotes) if there is a way to make the rectangles go in order of non-ascending height, otherwise print "NO".
You can print each letter in any case (upper or lower).
Sample 1
Inputcopy | Outputcopy |
---|---|
3 3 4 4 6 3 5 | YES |
Sample 2
Inputcopy | Outputcopy |
---|---|
2 3 4 5 5 | NO |
Note
In the first test, you can rotate the second and the third rectangles so that the heights will be [4, 4, 3].
In the second test, there is no way the second rectangle will be not higher than the first one.
- #include <iostream>
- #include <iomanip>
- #include <cstdio>
- #include <cmath>
- #include <string.h>
- #include <climits>
- #include <map>
- typedef long long ll;
- using namespace std;
- const double eps = 1e-7;
-
- map<int, pair<int,int>> mm;
- int n;
- int main()
- {
- cin >> n;
- for (int i = 1; i <= n; i++)
- {
- int num1, num2;
- cin >> num1 >> num2;
- mm[i] = make_pair(num1, num2);
- }
-
- int maxbian = max(mm[1].first, mm[1].second);
-
- for (int i = 2; i <= n; i++)
- {
- if (mm[i].first > maxbian && mm[i].second > maxbian)
- {
- cout << "NO" << endl;
- return 0;
- }
- else if (mm[i].first > maxbian) maxbian = mm[i].second;
- else if (mm[i].second > maxbian) maxbian = mm[i].first;
- else maxbian = max(mm[i].first, mm[i].second);
- }
- cout << "YES" << endl;
- return 0;
- }