Codeforces-1008B - Turn the Rectangles - 水題

題解鏈接

https://www.lucien.ink/archives/306/


題目鏈接

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


題目

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 (1n105 ) — the number of rectangles.

Each of the next n lines contains two integers wi and hi (1wi,hi109 ) — 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).

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.


題意

  給你n個長方形,問你能否通過任意多次旋轉,在不改變他們的相對順序的情況下讓他們的高度形成一個不上升序列。


實現

#include <bits/stdc++.h>
const int maxn = int(1e5) + 7;
typedef long long ll;
int x[maxn], y[maxn], n;
int main() {
    std::cin >> n;
    for (int i = 1; i <= n; i++) {
        std::cin >> x[i] >> y[i];
        if (x[i] > y[i]) std::swap(x[i], y[i]);
    }
    int upper = 0x3f3f3f3f;
    for (int i =1 ; i <= n; i++) {
        if (x[i] > upper) return 0 * puts("NO");
        if (y[i] <= upper) upper = y[i];
        else if (x[i] <= upper) upper = x[i];
    }
    puts("YES");
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章