CF 497 div 2 B
There are nn rectangles in a row. You can either turn each rectangle by 9090 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)
The first line contains a single integer nn (1≤n≤1051≤n≤105) — the number of rectangles.
Each of the next nn lines contains two integers wiwi and hihi (1≤wi,hi≤1091≤wi,hi≤109) — the width and the height of the ii-th rectangle.
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).
3
3 4
4 6
3 5
YES
2
3 4
5 5
NO
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.
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <utility>
#include <vector>
#include <map>
#include <queue>
#include <stack>
#include <cstdlib>
#include <cmath>
typedef long long ll;
#define lowbit(x) (x&(-x))
#define ls l,m,rt<<1
#define rs m+1,r,rt<<1|1
using namespace std;
const int N=1e5+9;
int w[N],h[N],c[N];
int n;
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%d%d",&w[i],&h[i]);
}
int i=1;
c[1]=max(w[1],h[1]);
for(int i=1;i+1<=n;i++){
if(c[i]>=max(w[i+1],h[i+1])){
c[i+1]=max(w[i+1],h[i+1]);
}
else if(c[i]>=min(w[i+1],h[i+1])){
c[i+1]=min(w[i+1],h[i+1]);
}
else{
printf("NO\n");
return 0;
}
}
printf("YES\n");
return 0;
}
/*
5
3 4
4 6
3 5
4 6
不能仅仅判断相邻两个的最大值、最小值关系,因为一旦前一个的最大值小于后一个的最大值并且大于后一个的最小值,那么后一个的高
就一定只能选最小值了。
*/
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现