Codeforces1008B——Turn the Rectangles

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 n lines 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
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 <cstdio>
#include <algorithm>
using namespace std;
int main(void){
    int n;
    int w,h;
    bool flag=false;
    int pre=0x3f3f3f3f;
    scanf("%d",&n);
    while(n--){
        scanf("%d%d",&w,&h);
        if(flag){
            continue;
        }
        if(max(h,w)<=pre){
            pre=max(h,w);
        }
        else if(min(h,w)<=pre){
            pre=min(h,w);
        }
        else{
            flag=true;
        }
    }
    if(flag){
        printf("NO\n");
    }
    else{
        printf("YES\n");
    }
    return 0;
}
全部评论

相关推荐

05-07 13:29
已编辑
门头沟学院 Java
北斗导航Compass低仿版:能不能先搞清楚优先级啊,怎么可能是项目问题,项目很重要吗?又没学历 又没实习大厂凭啥约面?那玩具项目 没应用在真实生产环境下的 就算做上天又有什么用?早点找个小公司实习 拿小公司实习去投大厂实习,这才是你现在该做的
投递美团等公司9个岗位 简历被挂麻了,求建议
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务