CF 1008B Turn the Rectangles(水题+贪心)

时间:2023-11-10 11:37:08

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

Input
3
3 4
4 6
3 5
Output
YES
Input
2
3 4
5 5
Output
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度,也就是长和宽可以转换,问这一些矩形能不能通过旋转实现按照高度非递增排列。

解题思路:在这道题中,我们对于矩形的高和长没有一个确切的概念,那么就用较长边和较短边来取代,我们需要得到一个按照一边非递增的数序列。我们需要尽可能的扩大数的范围,也就是说尽量用两边中较大的那个作为实现非递增序列的数因子,这样给了下一个矩形更大的发挥空间,如果较大的数超过上一个选择好的,那么只能选择较小的数了,如果较小的也超过了,说明不能实现。

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#define ll long long int
using namespace std;
struct rec
{
ll w;
ll h;
ll maxs;///较长边
ll mins;///较短边
} a[];
int main()
{
int n,i,flag;
ll x;
scanf("%d",&n);
for(i=; i<n; i++)///给矩形的两条边分类
{
scanf("%lld%lld",&a[i].w,&a[i].h);
if(a[i].w>=a[i].h)
{
a[i].maxs=a[i].w;
a[i].mins=a[i].h;
}
else
{
a[i].maxs=a[i].h;
a[i].mins=a[i].w;
}
}
flag=;
x=a[].maxs;
for(i=; i<n; i++)
{
if(a[i].maxs<=x)///更新较长边
{
x=a[i].maxs;
}
else if(a[i].maxs>x&&a[i].mins<=x)///使用较小边
{
x=a[i].mins;
}
else if(a[i].mins>x)///不符合要求
{
flag=;
break;
}
}
if(flag)
{
printf("YES\n");
}
else
{
printf("NO\n");
}
return ;
}