Codeforces Round #497 (Div. 2)--B. Turn the Rectangles

B. Turn the Rectangles
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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).

Input

The first line contains a single integer nn (1n1051≤n≤105) — the number of rectangles.

Each of the next nn lines contains two integers wiwi and hihi (1wi,hi1091≤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
input
Copy
3
3 4
4 6
3 5
output
Copy
YES
input
Copy
2
3 4
5 5
output
Copy
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.

题意:有很多个矩形,可以任意旋转(长高可以相互转换)。顺序不可变, 问有没有可以不升序排的方法,有就yes , 无就no。

我的蠢代码(被hacked掉了)

#include <stdio.h>
#include <iostream>
#include <map>
#include <bits/stdc++.h>
using namespace std;
struct node
{
	int l ;
	int r;
}s[100005];
int main()
{
	int t , a , b , n;
	int flag = 0;
	scanf("%d" , &n);
for(int i = 0 ; i < n ; i++)
{
	scanf("%d %d" , &s[i].l , &s[i].r);
}
for(int i = 0 ; i < n-1 ; i++)
{
	if(s[i+1].l <= s[i].l ||s[i+1].l <= s[i].r ||s[i+1].r <= s[i].l ||s[i+1].r <= s[i].r )
	{
		continue;
	}
	else
	{
		flag = 1;
	}
}
if(flag == 1)
{
	printf("NO\n");
	
}
else
{
	printf("YES\n");
}
	return 0;
}
可以AC的代码
#include <stdio.h>
#include <iostream>
#include <map>
#include <bits/stdc++.h>
using namespace std;
struct node
{
	int l ;
	int r;
}s[100005];
int main()
{
	int t , a , b , n;
	int flag = 0;
	scanf("%d" , &n);
for(int i = 0 ; i < n ; i++)
{
	scanf("%d %d" , &s[i].l , &s[i].r);
}
int PP = max(s[0].l,s[0].r);
for(int i = 0 ; i < n ; i++)
{
	int MIN = min(s[i].l,s[i].r);
	int MAX = max(s[i].l,s[i].r);
	if(PP >= MAX)
		{
			PP = MAX;
		}
		else if(PP >= MIN)
		{
			PP = MIN;
		}
		else if(PP < MIN)
		{
			flag = 1;
			break;
			
		}
}
if(flag == 1)
{
	printf("NO\n");
	
}
else
{
	printf("YES\n");
}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值