1. 程式人生 > >Codeforces-1008-B(水)

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;
}