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 n(1≤n≤105) — the number of rectangles.Each of the next nlines contains two integers wi and hi (1≤wi,hi≤109) — the width and the height of the i-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
题意:给你一些高和长已知的长方形,你可以将他们的高和长交换,但顺序不可打乱,问你能否使这些长方形的高 从左到右依次不递增
一道简单贪心题,在满足条件的情况下,使得该阶段下的三角形的高最大,若无法满足条件,则输出NO
#include <map>
#include <set>
#include <list>
#include <cmath>
#include <stack>
#include <queue>
#include <deque>
#include <cstdio>
#include <vector>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#define eps 1e-8
#define PI acos(-1)
#define INF 0x3f3f3f3f
#define N 200000 + 10
using namespace std;
const int intN = 3e5 + 10 ;
typedef long long int LL;
const int dir[4][2]= { {1,0},{0,1},{-1,0},{0,-1} };
int GCD(int a,int b)
{
return b ? GCD(b,a%b) : a;
}
struct REC
{
int w,h;
};
int main()
{
struct REC s[N];
int i,t=INF,n;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d%d",&s[i].w,&s[i].h);
for(i=0;i<n;i++)
{
if(s[i].w>t && s[i].h>t)
break;
if(s[i].w<=t && s[i].h>t)
t=s[i].w;
if(s[i].w>t && s[i].h<=t)
t=s[i].h;
if(s[i].w<=t && s[i].h<=t)
t=max(s[i].w,s[i].h);
}
if(i==n)
printf("YES\n");
else
printf("NO\n");
return 0;
}