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 (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 <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
不能仅仅判断相邻两个的最大值、最小值关系,因为一旦前一个的最大值小于后一个的最大值并且大于后一个的最小值,那么后一个的高
就一定只能选最小值了。
*/

 

CF 497 div 2 B的更多相关文章

  1. CF #376 (Div. 2) C. dfs

    1.CF #376 (Div. 2)    C. Socks       dfs 2.题意:给袜子上色,使n天左右脚袜子都同样颜色. 3.总结:一开始用链表存图,一直TLE test 6 (1)如果需 ...

  2. CF #375 (Div. 2) D. bfs

    1.CF #375 (Div. 2)  D. Lakes in Berland 2.总结:麻烦的bfs,但其实很水.. 3.题意:n*m的陆地与水泽,水泽在边界表示连通海洋.最后要剩k个湖,总要填掉多 ...

  3. CF #374 (Div. 2) D. 贪心,优先队列或set

    1.CF #374 (Div. 2)   D. Maxim and Array 2.总结:按绝对值最小贪心下去即可 3.题意:对n个数进行+x或-x的k次操作,要使操作之后的n个数乘积最小. (1)优 ...

  4. CF #374 (Div. 2) C. Journey dp

    1.CF #374 (Div. 2)    C.  Journey 2.总结:好题,这一道题,WA,MLE,TLE,RE,各种姿势都来了一遍.. 3.题意:有向无环图,找出第1个点到第n个点的一条路径 ...

  5. CF #371 (Div. 2) C、map标记

    1.CF #371 (Div. 2)   C. Sonya and Queries  map应用,也可用trie 2.总结:一开始直接用数组遍历,果断T了一发 题意:t个数,奇变1,偶变0,然后与问的 ...

  6. CF #365 (Div. 2) D - Mishka and Interesting sum 离线树状数组

    题目链接:CF #365 (Div. 2) D - Mishka and Interesting sum 题意:给出n个数和m个询问,(1 ≤ n, m ≤ 1 000 000) ,问在每个区间里所有 ...

  7. CF #365 (Div. 2) D - Mishka and Interesting sum 离线树状数组(转)

    转载自:http://www.cnblogs.com/icode-girl/p/5744409.html 题目链接:CF #365 (Div. 2) D - Mishka and Interestin ...

  8. Codeforces Round #497 (Div. 2)

    Codeforces Round #497 (Div. 2) https://codeforces.com/contest/1008 A #include<bits/stdc++.h> u ...

  9. CF#138 div 1 A. Bracket Sequence

    [#138 div 1 A. Bracket Sequence] [原题] A. Bracket Sequence time limit per test 2 seconds memory limit ...

随机推荐

  1. 多个activity之间的数据共享

    Activity之间的数据共享问题起初一看并没有那么纠结,原因在于两点,一来两个Activity之间可以通过回传的方式进行数据的共享,而哪怕是多个Activity之间,也可以通过静态类进行数据的共享. ...

  2. 关于IE和Firefox兼容性问题及解决办法

    1.//window.eventIE:有window.event对象FF:没有window.event对象.可以通过给函数的参数传递event对象.如onmousemove=doMouseMove(e ...

  3. nvcc 编译显示寄存器使用情况

    NVCC Compiler 里面增加 Command line pattern中${COMMAND}后 增加选项: --ptxas-options=-v

  4. https验证新发现-老知识

    HttpsURLConnection.setDefaultHostnameVerifier(hostnameVerifier); 可以设置https全局的域名校验规则 HttpsURLConnecti ...

  5. PHP函数:method_exists和function_exists

    method_exists 检查类的方法是否存在 bool method_exists ( mixed $object , string $method_name ) 检查类的方法是否存在于指定的ob ...

  6. Entity Framework --Entity SQL注意事项

    Entity SQL 是 ADO.NET 实体框架 提供的 SQL 类语言,用于支持 实体数据模型 (EDM).Entity SQL 可用于对象查询和使用 EntityClient 提供程序执行的查询 ...

  7. String和string

    String和string的区别 从位置讲:         1.String是.NET   Framework里面的String,小写的string是C#语言中的string 2.如果把using ...

  8. COGS 1191. [Tyvj Feb11] 猫咪的进化

    ★   输入文件:neko.in   输出文件:neko.out   简单对比时间限制:1 s   内存限制:128 MB [背景] 对于一只猫咪来说,它是有九条命的.但是并不是所有的猫咪都是这样,只 ...

  9. Python自学之路——自定义简单装饰器

    看了微信公众号推送的一道面试题,发现了闭包的问题,学习时间短,从来没有遇到过这种问题,研究一下. Python函数作用域 global:全局作用域 local:函数内部作用域 enclosing:函数 ...

  10. js高级笔录

    1.类型转换①转换成字符串toString() ⅰBoolean 值.数字和字符串的原始值的有趣之处在于它们是伪对象,这意味着它们实际上具有属性和方法. var sColor = "red& ...