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

發佈了135 篇原創文章 · 獲贊 12 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章