1008B Turn the Rectangles

B. Turn the Rectangles
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
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个矩形的长和宽,可以翻转每一个矩形,也就是交换长和宽,让矩形的长或者宽形成非递增排列。

扫描二维码关注公众号,回复: 2185264 查看本文章

题解:模拟   第一次取最大的,后面每次取的时候先拿最大的和前面一个比较,<=就存进去,如果不行就拿小的看是<=前面那个存进去,否则输出no

c++:

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,temp,a,b;
    cin>>n;
    cin>>a>>b;
    temp=max(a,b);
    for(int i=1; i<n; i++)
    {
        cin>>a>>b;
        if (temp>=max(a,b)) temp=max(a,b);
        else if (temp>=min(a,b)) temp=min(a,b);
        else
        {
            printf("NO");
            return 0;
        }
    }
    printf("YES");
    return 0;
}

python:

n=int(input())
s=[]
a,b=map(int,input().split())
s.append(max(a,b))
for i in range(1,n):
    a,b=map(int,input().split())
    if max(a,b)<=s[i-1]:
        s.append(max(b,a))
    elif min(a,b)<=s[i-1]:
        s.append(min(a,b))
    else:
        print("NO")
        exit()
print("YES")

猜你喜欢

转载自blog.csdn.net/memory_qianxiao/article/details/81040444