普及练习场 贪心EX 木棍加工

题目链接

题意理解

我第一眼感觉就是,这跟导弹拦截差不多啊,看懂导弹拦截就好了。

代码

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;

class Stick implements Comparable{
    int l;
    int w;

    public Stick(int l, int w) {
        this.l = l;
        this.w = w;
    }
    @Override
    public int compareTo(Object o) {
        Stick stick = (Stick)o;
        if(stick.l == this.l) {
            return stick.w - this.w;
        } else {
            return stick.l - this.l;
        }
    }
}

public class Main {
    static final int maxn = 5050;
    static Stick[] sticks;
    static int n;
    static int[] d = new int[maxn];
    static int m = 0;
    public static void main(String[] args) {
        FastScanner fs = new FastScanner();
        n = fs.nextInt();
        sticks = new Stick[n];
        for(int i = 0; i < n; i++) {
            int x = fs.nextInt();
            int y = fs.nextInt();
            sticks[i] = new Stick(x, y);
        }
        Arrays.sort(sticks);
        for(int i = 0; i < n; i++) {
            if(d[m] < sticks[i].w) {
                d[++m] = sticks[i].w;
            } else {
                d[lowerBound(d, 1, m + 1, sticks[i].w)] = sticks[i].w;
            }
        }
        System.out.println(m);
    }

    public static int lowerBound(int[] a, int l, int r, int key) {
        while (l < r) {
            int m = l + (r - l) / 2;
            if(a[m] >= key) {
                r = m;
            } else {
                l = m + 1;
            }
        }
        return  l;
    }

    public static class FastScanner {
        private BufferedReader br;
        private StringTokenizer st;

        public FastScanner() {
            br = new BufferedReader(new InputStreamReader(System.in));
        }

        public String nextToken() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        public int nextInt() {
            return Integer.valueOf(nextToken());
        }
    }
}

你可能感兴趣的:(洛谷,洛谷从零单刷)