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
3
3 4
4 6
3 5
YES
2
3 4
5 5
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度,也就是长和宽可以转换,问这一些矩形能不能通过旋转实现按照高度非递增排列。
解题思路:在这道题中,我们对于矩形的高和长没有一个确切的概念,那么就用较长边和较短边来取代,我们需要得到一个按照一边非递增的数序列。我们需要尽可能的扩大数的范围,也就是说尽量用两边中较大的那个作为实现非递增序列的数因子,这样给了下一个矩形更大的发挥空间,如果较大的数超过上一个选择好的,那么只能选择较小的数了,如果较小的也超过了,说明不能实现。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#define ll long long int
using namespace std;
struct rec
{
ll w;
ll h;
ll maxs;///较长边
ll mins;///较短边
} a[];
int main()
{
int n,i,flag;
ll x;
scanf("%d",&n);
for(i=; i<n; i++)///给矩形的两条边分类
{
scanf("%lld%lld",&a[i].w,&a[i].h);
if(a[i].w>=a[i].h)
{
a[i].maxs=a[i].w;
a[i].mins=a[i].h;
}
else
{
a[i].maxs=a[i].h;
a[i].mins=a[i].w;
}
}
flag=;
x=a[].maxs;
for(i=; i<n; i++)
{
if(a[i].maxs<=x)///更新较长边
{
x=a[i].maxs;
}
else if(a[i].maxs>x&&a[i].mins<=x)///使用较小边
{
x=a[i].mins;
}
else if(a[i].mins>x)///不符合要求
{
flag=;
break;
}
}
if(flag)
{
printf("YES\n");
}
else
{
printf("NO\n");
}
return ;
}
CF 1008B Turn the Rectangles(水题+贪心)的更多相关文章
- CF#FF(255)-div1-C【水题,枚举】
[吐槽]:本来没打算写这题的题解的,但惨不忍睹得WA了13次,想想还是记录一下吧.自己的“分类讨论能力”本来就很差. 刚开始第一眼扫过去以为是LIS,然后忽略了复杂度,果断TLE了,说起来也好惭愧,也 ...
- CodeForces 719B Anatoly and Cockroaches (水题贪心)
题意:给定一个序列,让你用最少的操作把它变成交替的,操作有两种,任意交换两种,再就是把一种变成另一种. 析:贪心,策略是分别从br开始和rb开始然后取最优,先交换,交换是最优的,不行再变色. 代码如下 ...
- Codeforces Round #303 (Div. 2) D. Queue 水题贪心
题目: 题意:给你n个数值,要求排列这个序列使得第k个数值的前K-1个数的和>=第k个数值的个数尽可能多: #include <iostream> #include <cstd ...
- CF 277.5 A.SwapSort 水题
//STL教你做人系列 #include<stdio.h> #include<iostream> #include<math.h> #include<algo ...
- 洛谷p1208 水题贪心 思想入门
题目描述 由于乳制品产业利润很低,所以降低原材料(牛奶)价格就变得十分重要.帮助Marry乳业找到最优的牛奶采购方案. Marry乳业从一些奶农手中采购牛奶,并且每一位奶农为乳制品加工企业提供的价格是 ...
- codeforces 637D D. Running with Obstacles(dp,水题,贪心)
题目链接: D. Running with Obstacles time limit per test 2 seconds memory limit per test 256 megabytes in ...
- codeforces 515C C. Drazil and Factorial(水题,贪心)
题目链接: C. Drazil and Factorial time limit per test 2 seconds memory limit per test 256 megabytes inpu ...
- CF 593B Anton and Lines(水题)
题意是给你n条直线,和x1,x2;问 在x1,x2之间(不包括在x1,x2上) 存不存在任意两条线的交点. 说思路,其实很简单,因为给的直线的条数很多,所以无法暴力求每两条直线的交点,那么就求每条直线 ...
- CF 628B New Skateboard --- 水题
CD 628B 题目大意:给定一个数字(<=3*10^5),判断其能被4整除的连续子串有多少个 解题思路:注意一个整除4的性质: 若bc能被4整除,则a1a2a3a4...anbc也一定能被4整 ...
随机推荐
- pt-archiver数据归档
可以使用percona-toolkit包中的pt-archiver工具来进行历史数据归档 pt-archiver使用的场景: 1.清理线上过期数据. 2.清理过期数据,并把数据归档到本地归档表中,或者 ...
- centos 7 配置nginx 的yum源
在/etc/yum.repos.d里创建nginx.repo文件: touch nginx.repo vim nginx.repo 填写如下内容后保存 [nginx] name=nginx repo ...
- vue数组赋值
在使用vue开发移动端项目过程中,统一数组在对多个变量赋值时:希望一个数组的改变不影响另外一个数组,此时可以使用如下方式实现: let arr = [] let a1 = JSON.parse(JSO ...
- 追溯了解Ubuntu之------基本命令操作(叁)
在使用Ubuntu中的一些基本命令与Linux中是有区别的: 1. 查看Ubuntu系统位数:uname -ar 或 getconf LONG_BIT 2. 获取Ubu ...
- Jquery中select使用
select获取当前选中的value $('#DDLDEP').change(function () { var depId = $(this).children('option:selected') ...
- linux环境mysql的安装主从关系的配置
- [FreeRTOS入门] 1.CubeMX中FreeRTOS配置参数及理解
1.有关优先级 1.1 Configuration --> FreeRTOS MAX_PRIORITIES 设置任务优先级的数量:配置应用程序有效的优先级数目.任何数量的任务都可以共享一个优先级 ...
- GC错误
如果出现GC错误,可设置客户端 set mapreduce.map.java.opts 设置一下 R的GC错误,在顶端设置这个参数 options(java.parameters = "-X ...
- poj 2739 Sum of Consecutive Prime Numbers 尺取法
Time Limit: 1000MS Memory Limit: 65536K Description Some positive integers can be represented by a ...
- 20155304 2016-2017-2《Java程序设计》课程总结
20155304 2016-2017-2<Java程序设计>课程总结 (按顺序)每周作业链接汇总 预备作业1:对自己专业看法及.学习Java的期望,以及心中的师生关系 预备作业2:有关技能 ...