Skip to content

14147: 【原4147】翻转长方形

题目

题目描述

author: Yifan 原OJ链接:https://acm.sjtu.edu.cn/OnlineJudge-old/problem/4147

Description

现在有n个长方形排成一列。对任一长方形,你可以将其翻转90度(即长方形的高度和宽度互换),也可以不对它进行操作。但是你不能改变长方形放置的顺序。

请问是否能使得这一列长方形的高度呈非递增序列(即每个长方形的高度都不会超过它之前的长方形的高度)?

Input Format

第一行是长方形的个数n,1 <= n <= 100000。

接下来n行每行两个整数wi和hi,表示第i个长方形的宽度和高度,1 <= wi, hi <= 10^9。

Output Format

如果可以使这一列长方形的高度呈非递增序列,输出YES,否则输出NO。

Sample1 Input

3
3 4
4 6
3 5

Sample1 Output

YES

Sample2 Input

2
3 4
5 5

Sample2 Output

NO

BugenZhao's solution

//
// Created by BugenZhao on 2019/3/16.
//
// 娱乐

#include <iostream>
#include <algorithm>

using namespace std;

int main() {
    int n;
    int w, h;
    int max;
    int currHeight = 0x7fffffff;
    cin >> n;
    for (int i = 0; i < n; ++i) {
        cin >> w >> h;
        if (w > h) {
            swap(w, h);
        }
        if (currHeight < w) {
            cout << "NO" << endl;
            return 0;
        } else if (currHeight < h) {
            currHeight = w;
        } else {
            currHeight = h;
        }
    }
    cout << "YES" << endl;
    return 0;
}

ligongzzz's solution

#include "iostream"
#include "cstdio"
using namespace std;

constexpr int inf = (int)(1e9 + 7);

int main() {
    int n;
    scanf("%d", &n);

    for (int i = 0, last = inf; i < n; ++i) {
        int wi, hi;
        scanf("%d %d", &wi, &hi);
        if (wi <= last && hi <= last) {
            last = wi > hi ? wi : hi;
        }
        else if (wi <= last) {
            last = wi;
        }
        else if (hi <= last) {
            last = hi;
        }
        else {
            cout << "NO";
            return 0;
        }
    }
    cout << "YES";

    return 0;
}

skyzh's solution

#include <iostream>
#include <cstdio>
#include <climits>
using namespace std;

int main() {
    int n, wi, hi;
    int c_H = INT_MAX;
    scanf("%d", &n);
    bool ok = true;
    for (int i = 0; i < n; i++) {
        scanf("%d%d", &wi, &hi);
        int c_min = min(wi, hi);
        int c_max = max(wi, hi);
        if (c_min > c_H) {
            ok = false;
        } else {
            if (c_max <= c_H) c_H = c_max;
            else c_H = c_min;
        }
    }
    if (ok) {
        cout << "YES" << endl;
    } else {
        cout << "NO" << endl;
    }
    return 0;
}