CF 1008B 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 nn (1≤n≤105) — the number of rectangles.

Each of the next nn lines contains two integers wiwi and hihi (1≤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

33 44 63 5

Output

YES

Input

23 45 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

题目意思:按顺序给你n个矩形,这些矩形可以旋转90度,也就是长和宽可以转换,问这一些矩形能不能通过旋转实现按照高度非递增排列。

解题思路:在这道题中,我们对于矩形的高和长没有一个确切的概念,那么就用较长边和较短边来取代,我们需要得到一个按照一边非递增的数序列。我们需要尽可能的扩大数的范围,也就是说尽量用两边中较大的那个作为实现非递增序列的数因子,这样给了下一个矩形更大的发挥空间,如果较大的数超过上一个选择好的,那么只能选择较小的数了,如果较小的也超过了,说明不能实现。

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 #include <string>
 6 #define ll long long int
 7 using namespace std;
 8 struct rec
 9 {
10     ll w;
11     ll h;
12     ll maxs;///较长边
13     ll mins;///较短边
14 } a[100010];
15 int main()
16 {
17     int n,i,flag;
18     ll x;
19     scanf("%d",&n);
20     for(i=0; i<n; i++)///给矩形的两条边分类
21     {
22         scanf("%lld%lld",&a[i].w,&a[i].h);
23         if(a[i].w>=a[i].h)
24         {
25             a[i].maxs=a[i].w;
26             a[i].mins=a[i].h;
27         }
28         else
29         {
30             a[i].maxs=a[i].h;
31             a[i].mins=a[i].w;
32         }
33     }
34     flag=1;
35     x=a[0].maxs;
36     for(i=1; i<n; i++)
37     {
38        if(a[i].maxs<=x)///更新较长边
39        {
40            x=a[i].maxs;
41        }
42        else if(a[i].maxs>x&&a[i].mins<=x)///使用较小边
43        {
44            x=a[i].mins;
45        }
46        else if(a[i].mins>x)///不符合要求
47        {
48            flag=0;
49            break;
50        }
51     }
52     if(flag)
53     {
54         printf("YES\n");
55     }
56     else
57     {
58         printf("NO\n");
59     }
60     return 0;
61 }

原文地址:https://www.cnblogs.com/wkfvawl/p/9740682.html

时间: 2024-08-01 10:34:49

CF 1008B Turn the Rectangles(水题+贪心)的相关文章

CF#FF(255)-div1-C【水题,枚举】

[吐槽]:本来没打算写这题的题解的,但惨不忍睹得WA了13次,想想还是记录一下吧.自己的“分类讨论能力”本来就很差. 刚开始第一眼扫过去以为是LIS,然后忽略了复杂度,果断TLE了,说起来也好惭愧,也说明有时候太懒得动脑了,总是习惯利用惯性思维,这不是一件好事. [题意]:给你大小为n的整型数组a[n],求这数组的一个子串,其中最多可以修改子串中的一个数字,使得到的子串是最长的严格递增的子串,输出该子串的长度 L. [思路]:O(n)复杂度,枚举断点情况.第0个和第n个位置默认为断点.(用ve[

CodeForces 719B Anatoly and Cockroaches (水题贪心)

题意:给定一个序列,让你用最少的操作把它变成交替的,操作有两种,任意交换两种,再就是把一种变成另一种. 析:贪心,策略是分别从br开始和rb开始然后取最优,先交换,交换是最优的,不行再变色. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cstdlib> #include <c

51nod 1344 走格子(水题+贪心)

1344 走格子 基准时间限制:1 秒 空间限制:131072 KB 分值: 5 难度:1级算法题 收藏 关注 取消关注 有编号1-n的n个格子,机器人从1号格子顺序向后走,一直走到n号格子,并需要从n号格子走出去.机器人有一个初始能量,每个格子对应一个整数A[i],表示这个格子的能量值.如果A[i] > 0,机器人走到这个格子能够获取A[i]个能量,如果A[i] < 0,走到这个格子需要消耗相应的能量,如果机器人的能量 < 0,就无法继续前进了.问机器人最少需要有多少初始能量,才能完成

CF 277.5 A.SwapSort 水题

//STL教你做人系列 #include<stdio.h> #include<iostream> #include<math.h> #include<algorithm> using namespace std; int n,a[3100]; int main() { cin>>n; for(int i=0;i<n;i++) cin>>a[i]; cout<<n<<endl; for(int i=0;i&

CF 628A --- Tennis Tournament --- 水题

CF 628A 题目大意:给定n,b,p,其中n为进行比赛的人数,b为每场进行比赛的每一位运动员需要的水的数量, p为整个赛程提供给每位运动员的毛巾数量, 每次在剩余的n人数中,挑选2^k=m(m <=n)个人进行比赛,剩余的n-m个人直接晋级, 直至只剩一人为止,问总共需要的水的数量和毛巾的数量 解题思路:毛巾数很简单: n*p即可 水的数量:1,2,4,8,16,32,64,128,256,512,提前打成一个表, 根据当前剩余的人数n在表中二分查找最大的小于等于n的数,结果即为本次进行比赛

HDU1009_FatMouse&amp;#39; Trade【贪心】【水题】

FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 44470    Accepted Submission(s): 14872 Problem Description FatMouse prepared M pounds of cat food, ready to trade with the cats g

CF 628B New Skateboard --- 水题

CD 628B 题目大意:给定一个数字(<=3*10^5),判断其能被4整除的连续子串有多少个 解题思路:注意一个整除4的性质: 若bc能被4整除,则a1a2a3a4...anbc也一定能被4整除: 利用这个性质,先特判第一位数字是否能被4整除,可以则++cnt, 之后从第二位数字开始,设当前位为i,先判断a[i]能否被4整除,可以则++cnt, 再判断a[i-1]*10+a[i]能否被4整除,可以则cnt = cnt + (i) 相关证明: 设一整数各个位置为a1,a2,a3,...,an,b

LightOJ 1166 Old Sorting 置换群 或 贪心 水题

LINK 题意:给出1~n数字的排列,求变为递增有序的最小交换次数 思路:水题.数据给的很小怎么搞都可以.由于坐标和数字都是1~n,所以我使用置换群求循环节个数和长度的方法. /** @Date : 2017-07-20 14:45:30 * @FileName: LightOJ 1166 贪心 或 置换群 水题.cpp * @Platform: Windows * @Author : Lweleth ([email protected]) * @Link : https://github.co

HDU1009_FatMouse&#39; Trade【贪心】【水题】

FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 44470    Accepted Submission(s): 14872 Problem Description FatMouse prepared M pounds of cat food, ready to trade with the cats g