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
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.

 

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <utility>
#include <vector>
#include <map>
#include <queue>
#include <stack>
#include <cstdlib>
#include <cmath>
typedef long long ll;
#define lowbit(x) (x&(-x))
#define ls l,m,rt<<1
#define rs m+1,r,rt<<1|1
using namespace std;
const int N=1e5+9;
int w[N],h[N],c[N];
int n;
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%d%d",&w[i],&h[i]);
}
int i=1;
c[1]=max(w[1],h[1]);
for(int i=1;i+1<=n;i++){
if(c[i]>=max(w[i+1],h[i+1])){
c[i+1]=max(w[i+1],h[i+1]);
}
else if(c[i]>=min(w[i+1],h[i+1])){
c[i+1]=min(w[i+1],h[i+1]);
}
else{
printf("NO\n");
return 0;
}
}
printf("YES\n");
return 0;
}

 

 


/*
5
3 4
4 6
3 5
4 6
不能仅仅判断相邻两个的最大值、最小值关系,因为一旦前一个的最大值小于后一个的最大值并且大于后一个的最小值,那么后一个的高
就一定只能选最小值了。
*/