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整 ...
随机推荐
- PHP程序员学Objective-C之后的变化
趣味坎谈,不一定100%准确,以自己的实际情况为准; 如题,我2008年开始学PHP,PHP是我学的第二门编程语言,一直用到现在,2010年初开始做iOS开发,学习了Objective-C,学这2门语 ...
- redis应用场景:实现简单计数器-防止刷单
redis应用场景:实现计数器-防止刷单 最近由于双11要来临,公司需要在接口请求上,做一下并发限制的处理,或者做一个防止刷单的安全拦截:比如:一个接口请求,限制每秒请求总数为200次,超过200次就 ...
- Golang 对接宝付、通联、富友金账户...填坑记
一.宝付私钥加密,公钥解密 由于对RSA加密解密原理不是很熟悉,宝付也没有Golang的Demo提供.Go语言库里一般都是私钥解密.公钥加密,或者私钥签名.公钥验签.宝付需要反过来,这里也到好找到了h ...
- angular中的$cookies和$cookieStore设置过期时间
angular1.4及以上版本才支持$cookies. 项目引入的是1.4.2版本,操作cookies原先一直用的是$cookieStore,用的飞起啊. $cookieStore.remove(&q ...
- SQL学习笔记:函数
SQL函数 AVG select AVG(col) AS avgvalue from tablename select col2 from tablename where col1>(selec ...
- Java六大设计原则
类的设计原则 依赖倒置原则-Dependency Inversion Principle (DIP) 里氏替换原则-Liskov Substitution Principle (LSP) 接口 ...
- 20155218 《Java程序设计》实验二(Java面向对象程序设计)实验报告
20155218 <Java程序设计>实验二(Java面向对象程序设计)实验报告 一.实验内容及步骤 (一)单元测试 主要学习安装和使用junit来测试编写的程序,并学习以TDD(Test ...
- # 20155224 课堂实践 MyOD
20155224 课堂实践 MyOD 要求 编写MyOD.java 用java MyOD XXX实现Linux下od -tx -tc XXX的功能 提交测试代码和运行结果截图,加上学号水印,提交码云代 ...
- 20155234 2006-2007-2 《Java程序设计》第4周学习总结
20155234 2006-2007-2 <Java程序设计>第4周学习总结 教材学习内容总结 为了避免重复的行为定义使用继承. 要学会如何正确判断使用继承的时机以及继承之后如何活用多态. ...
- 2016-2017-2 20155302 实验四 Android 开发基础
2016-2017-2 20155302 实验四 Android 开发基础 实验内容 1.下载和安装Android Studio 2.学会使用Android Studio进行简单的Android开发 ...