codeforces 解题报告 1008B. Turn the Rectangles 暴力贪心

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

解题思路:

1.可以交换每一行的两个数,最终使得右边那列非增

2.暴力遍历,以行为组,在不大于前一组的前提下,使得 h[i] 取两数中较大值

3.最后检查序列是否非增即可

import java.util.Scanner;

public class Main {

    public static void main(String args[]) {

        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[] w = new int[100010];
        int[] h = new int[100010];
        for(int i = 1;i <= n;i++) {
            w[i] = sc.nextInt();
            h[i] = sc.nextInt();
        }
        h[1] = Math.max(h[1],w[1]);          //h[i]尽可能选大
        int tag = h[1];
        for(int i = 2;i <= n;i++) {          //h[i]在比h[i-1]的前提下,尽可能选大
            if(Math.max(h[i],w[i]) <= tag) {
                h[i] = Math.max(h[i],w[i]);
            } else {
                h[i] = Math.min(h[i],w[i]);
            }
            tag = h[i];
        }
        int i;
        for(i = 1;i < n;i++) {               //判断处理好的h序列是否非增
            if(h[i] < h[i + 1]) {
                break;
            }
        }
        if (i == n) {
            System.out.println("YES");
        } else {
            System.out.println("NO");
        }
    }
}

猜你喜欢

转载自blog.csdn.net/a912952381/article/details/81040150