Turn the Rectangles (水题)

本文介绍了一个有趣的问题:如何通过旋转矩形使其高度按非升序排列。提供了算法思路及实现代码,通过比较每个矩形的高度和宽度来决定是否旋转。

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.

每次选择小于等于前一个的较大值,如果找不到就是no,如果完成循环,就是yes

code:

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn = 1e5+10;
int n;
int re[maxn][2];
int s[maxn];
int top = -1;
int main(){
    cin >> n;
    for(int i = 0; i < n; i++){
        scanf("%d%d",&re[i][0],&re[i][1]);
    }
    s[++top] = max(re[0][0],re[0][1]);
    int flag = 1;
    for(int i = 1; i < n; i++){
        int tmp = -1;
        if(re[i][0] <= s[top]) tmp = re[i][0];
        if(re[i][1] <= s[top]) tmp = re[i][1];
        if(re[i][0] <= s[top] && re[i][1] <= s[top]) tmp = max(re[i][0],re[i][1]);
        if(tmp == -1){
            flag = 0;
            break;
        }
        s[++top] = tmp;
    }
    if(flag) printf("YES\n");
    else printf("NO\n");
    return 0;
}
### 实现圆角矩形的技术解析 在图形设计和编程领域,绘制带有圆角的矩形是一项常见需求。无论是用于界面美化还是特定视觉效果,掌握这一技能都至关重要。以下是关于如何通过多种编程语言和技术实现 **rounded rectangles** 的详细介绍。 --- #### Java Swing 中的圆角矩形绘制 在 Java 的 `Graphics2D` 类中,提供了专门的方法来绘制带圆角的矩形——即 `drawRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight)` 方法[^1]。此方法允许开发者指定矩形的位置 (`x`, `y`)、尺寸 (`width`, `height`) 以及弧度参数 (`arcWidth`, `arcHeight`)。这两个弧度参数决定了拐角处圆滑程度。 ```java import java.awt.*; import javax.swing.*; public class RoundedRectangleExample extends JPanel { @Override protected void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2d = (Graphics2D)g; g2d.setColor(Color.BLUE); // 参数解释:(起点X坐标, 起点Y坐标, 宽度, 高度, X方向半径, Y方向半径) g2d.drawRoundRect(50, 50, 200, 100, 30, 30); g2d.fillRoundRect(80, 170, 150, 60, 20, 20); } public static void main(String[] args) { JFrame frame = new JFrame("Rounded Rectangle Example"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(new RoundedRectangleExample()); frame.setSize(350, 350); frame.setVisible(true); } } ``` 上述代码展示了两种操作:一种是仅勾勒轮廓线(`drawRoundRect`),另一种则是填充颜色区域(`fillRoundRect`)。这两种方式均依赖相同的六个核心参数设置。 --- #### CSS 中的圆角矩形样式 除了编程语言外,在前端开发中的 HTML/CSS 技术也可以轻松创建圆角矩形。CSS 提供了属性 `border-radius` 来控制边框四角的弯曲程度。其语法如下所示: ```css div.rounded-rectangle { border: 2px solid black; /* 边界 */ padding: 20px; /* 内部间距 */ background-color: lightblue; /* 填充背景色 */ /* 设置四个角均为相同曲率 */ border-radius: 25px; /* 或单独定义每个角落的不同曲率 */ /* border-top-left-radius: 10px; border-top-right-radius: 20px; border-bottom-right-radius: 30px; border-bottom-left-radius: 40px; */ } ``` 配合 HTML 结构即可渲染出具有圆润边缘的效果盒子容器。 --- #### Python Turtle 图形库的应用 Python 的标准库之一 `Turtle` 同样能够用来制作简单的二维矢量艺术作品,其中包括圆角矩形这样的基础形状。虽然它并没有内置直接支持 round rect 功能,但我们可以通过分步画法模拟出来。 ```python import turtle def draw_round_rectangle(turt, w, h, r): turt.begin_fill() for _ in range(2): # 上下半部分循环两次 turt.forward(w - 2*r) # 移动直线距离减去两倍半径长度 turt.circle(r, extent=90) # 绘制四分之一圆作为转角过渡 turt.forward(h - 2*r) turt.circle(r, extent=90) turt.end_fill() screen = turtle.Screen() t = turtle.Turtle() # 开始绘画前准备动作 t.penup() t.goto(-100,-50) t.pendown() t.color('red', 'yellow') # 笔迹红色内部黄色填满 draw_round_rectangle(t, 200, 100, 30) turtle.done() ``` 这里自定义函数接受三个必要输入值宽度(`w`)高度(`h`)还有决定弯道平顺性的半径(`r`)之后按照预定路径逐步拼接而成最终目标图案。 --- #### OLED 屏幕上的 Arduino 应用案例 针对嵌入式设备如Arduino连接的小型显示屏来说,借助第三方驱动程序包也能达成相似目的。例如 Adafruit_SSD1306 和 Adafruit_GFX 这两个开源项目共同协作便能实现在 I²C 控制下的 SSD1306 显示器上面呈现各种几何形态包括但不限于圆形椭圆乃至我们的主—圆角矩形[^4]。 典型初始化流程大致如此: 1. 下载安装所需软件组件; 2. 初始化硬件串行通讯链路; 3. 调用相应 API 函数完成定制显示内容编辑工作; 具体示例参见官方文档说明链接地址... --- ### 总结 无论采用何种工具栈或者框架体系架构,只要理解背后数学原理加上灵活运用各自平台特色API接口就能顺利解决此类问。希望以上分享对你有所帮助! ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值