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整 ...
随机推荐
- 【python 虚拟环境 virtualenv的配置】
该目录内生成一个虚目录: #运行activcate下的shell脚本,激活虚拟环境 #pip python包管理器
- Spring retry实践
在开发中,重试是一个经常使用的手段.比如MQ发送消息失败,会采取重试手段,比如工程中使用RPC请求外部服务,可能因为网络波动出现超时而采取重试手段......可以看见重试操作是非常常见的一种处理问题, ...
- php文件相关操作
//遍历目录及文件 function myBianli($dirname){ //1.打开 $dir = opendir($dirname); //2.读取 while($filename = rea ...
- Linux IO多路复用 select
Linux IO多路复用 select 之前曾经写过简单的服务器,服务器是用多线程阻塞,客户端每一帧是用非阻塞实现的 后来发现select可以用来多路IO复用,就是说可以把服务器这么多线程放在一个线程 ...
- 检测com端口代码实现
1:scan HRESULT CDevHound::Scan(const vector<CString> &guiInfo, vector<DEV_INFO> & ...
- spark执行命令 监控执行命令
#!/bin/bash #/usr/hdp/current/flume-server/bin/flume-ng agent -c conf/ -f /usr/hdp/current/flume-ser ...
- ubuntu配置机器学习环境(二) cuda 和cudnn 安装
Nvidia CUDA Toolkit的安装(cuda) PS:特别推荐*.deb的方法,目前已提供离线版的deb文件,该方法比较简单,不需要切换到tty模式,因此不再提供原来的*.run安装方法,这 ...
- 纪中OJ 2019.01.25【NOIP提高组】模拟 B 组 T2 数字对
声明 数字对 Time Limits: 2000 ms Memory Limits: 262144 KB Description 小 H 是个善于思考的学生,现在她又在思考一个有关序列的问题. ...
- mybatsi中文乱码问题
乱码问题:待总结,这里先贴出网友的博客: http://blog.csdn.net/zht666/article/details/8955952
- 20155232 实验二《Java面向对象程序设计》实验报告
20155232 实验一<Java面向对象程序设计>实验报告 实验内容 1.初步掌握单元测试和TDD 2.理解并掌握面向对象三要素:封装.继承.多态 3.初步掌握UML建模 4.熟悉S.O ...