Codeforces-1008-B(水)

Codeforces 1008B - Turn the Rectangles

题目原址

[http://codeforces.com/contest/1008/problem/B]

题意

输入一个数 n,接下来 n 对数 w i w_{i} , h i h_{i} , 你可以交换其中的 w i w_{i} h i h_{i} 使得数组 h n h_{n} 为广义单调递减。

题解

用另一变量储存 h i 1 h_{i-1} 通过对 w i w_{i} h i h_{i} 比较,选出符合广义单调递减的最大值作为新的 h i 1 h_{i-1} ,当不存在时,返回 NO 。

实现

#include <stdio.h>
int main(){
	int n;
	scanf("%d",&n);
	int w,h;
	int t=0;//储存上次的h

	while(n--){
		scanf("%d %d",&w,&h);
		if(w > h)
			w^=h^=w^=h; //交换wh,使得h>=w
		if(t >= h || !t)//如果h小于等于上次的h,或者这是第一次
			t = h;
		else if(t >= w)
			t = w;
		else
			return 0*printf("NO\n");
	}
	printf("YES\n");
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_43406046/article/details/83956769