Turn the Rectangles 1008B

output
standard output

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 (1n1051≤n≤105) — the number of rectangles.

Each of the next nn lines contains two integers wiwi and hihi (1wi,hi1091≤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
input
Copy
3
3 4
4 6
3 5
output
Copy
YES
input
Copy
2
3 4
5 5
output
Copy
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度,可以旋转多次或者不旋转,但是你不能改变矩形的排列顺序,每一个矩形不能比它前一个矩形高,如果可以输出YES,否则输出NO。

分析:使第一个矩形的高最大,如果下一个矩形的宽和高的最小值小于它前一个矩形的高,比较:1,(1)如果宽大于前一个的高,则把该矩形的高作为高(2)如果高大于前一个矩形的高,则把该矩形的宽作为该矩形的高(3)如果宽和高均小于前一个的高,则找出宽和高较大的那个作为该矩形的高   2,如果该矩形的宽高最小值大于前一个矩形的高,那么直接输出NO

 1 #include<cstdio>
 2 #include<algorithm>
 3 using namespace std;
 4 int main()
 5 {
 6     int n,w[100005],h[100005];
 7     while(~scanf("%d",&n))
 8     {
 9         for(int i=1; i<=n; i++)
10         {
11             scanf("%d %d",&w[i],&h[i]);
12         }
13         int t1,t2,i;
14         for(i=1; i<=n; i++)
15         {
16             if(i==1)
17                 t1=max(w[i],h[i]);
18             else
19             {
20                 t2=min(w[i],h[i]);
21                 if(t2<=t1)
22                 {
23                     if(w[i]>t1)
24                         t1=h[i];
25                     else if(h[i]>t1)
26                         t1=w[i];
27                     else if(w[i]<=t1&&h[i]<=t1)
28                         t1=max(w[i],h[i]);
29                 }
30                 else
31                 {
32                     break;
33                 }
34             }
35 
36         }
37         if(i==n+1)
38             printf("YES
");
39         else
40             printf("NO
");
41     }
42     return 0;
43 }
 
原文地址:https://www.cnblogs.com/LLLAIH/p/9698523.html