2018学校暑期集训第二天——ACM基础算法
练习题B —— CodeForces - 1008B
Turn the Rectangles
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 (1≤n≤1051≤n≤105) — the number of rectangles.
Each of the next nn lines contains two integers wiwi and hihi (1≤wi,hi≤1091≤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<algorithm>
#include<string>
#include<vector>
#include<cstdio>
#include<cmath>
#include<set>
#include<map>
using namespace std;
struct Point{
int a,b;
}t[100010];
int main(void)
{
int n;
scanf("%d", &n);
for(int i=0;i<n;i++)
cin >> t[i].a >> t[i].b;
int maxn=max(t[0].a,t[0].b);
int jua,jui;
bool judge=1;
for(int i=1;i<n;i++){
jua=max(t[i].a,t[i].b);
if(jua<=maxn){
maxn=jua;
}else{
jui=min(t[i].a,t[i].b);
if(jui<=maxn){
maxn=jui;
}else{
judge=0;
break;
}
}
}
if(judge)
cout << "YES";
else
cout << "NO";
return 0;
}