There are n rectangles in a row. You can either turn each rectangle by 90 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≤n≤105) — the number of rectangles.
Each of the next nn lines contains two integers wiwi and hihi (1≤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).
Examples
3
3 4
4 6
3 5
YES
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
题目意思:按顺序给你n个矩形,这些矩形可以旋转90度,也就是长和宽可以转换,问这一些矩形能不能通过旋转实现按照高度非递增排列。
解题思路:在这道题中,我们对于矩形的高和长没有一个确切的概念,那么就用较长边和较短边来取代,我们需要得到一个按照一边非递增的数序列。我们需要尽可能的扩大数的范围,也就是说尽量用两边中较大的那个作为实现非递增序列的数因子,这样给了下一个矩形更大的发挥空间,如果较大的数超过上一个选择好的,那么只能选择较小的数了,如果较小的也超过了,说明不能实现。
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 #include <string> 6 #define ll long long int 7 using namespace std; 8 struct rec 9 { 10 ll w; 11 ll h; 12 ll maxs;///较长边 13 ll mins;///较短边 14 } a[100010]; 15 int main() 16 { 17 int n,i,flag; 18 ll x; 19 scanf("%d",&n); 20 for(i=0; i<n; i++)///给矩形的两条边分类 21 { 22 scanf("%lld%lld",&a[i].w,&a[i].h); 23 if(a[i].w>=a[i].h) 24 { 25 a[i].maxs=a[i].w; 26 a[i].mins=a[i].h; 27 } 28 else 29 { 30 a[i].maxs=a[i].h; 31 a[i].mins=a[i].w; 32 } 33 } 34 flag=1; 35 x=a[0].maxs; 36 for(i=1; i<n; i++) 37 { 38 if(a[i].maxs<=x)///更新较长边 39 { 40 x=a[i].maxs; 41 } 42 else if(a[i].maxs>x&&a[i].mins<=x)///使用较小边 43 { 44 x=a[i].mins; 45 } 46 else if(a[i].mins>x)///不符合要求 47 { 48 flag=0; 49 break; 50 } 51 } 52 if(flag) 53 { 54 printf("YES "); 55 } 56 else 57 { 58 printf("NO "); 59 } 60 return 0; 61 }