nlogn LIS

主要思想是维护一个b数组。b[i]代表长度为i的LIS的最后一位最小为b[i]。因此b[i]满足单调性,可以进行二分。
代码(java):

import java.io.*;
import java.lang.reflect.Array;
import java.rmi.*;
import java.util.*;
import java.math.*;
import java.lang.*;
import java.security.*;

public class Main {

    static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    static StringTokenizer tok;

    static boolean hasNext() {
        while (tok == null || !tok.hasMoreTokens())
            try {
                tok = new StringTokenizer(in.readLine());
            } catch (Exception e) {
                return false;
            }
        return true;
    }

    static String next() {
        hasNext();
        return tok.nextToken();
    }

    static String nextLine() {
        try {
            return in.readLine();
        } catch (Exception e) {
            return null;
        }
    }

    static long nextLong() {
        return Long.parseLong(next());
    }

    static int nextInt() {
        return Integer.parseInt(next());
    }

    static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));

    static int n, a[], b[];

    static int lower_bound(int l, int r, int x) {
        while (l < r) {
            int mid = (l + r + 1) >> 1;
            if (b[mid] <= x) l = mid;
            else r = mid - 1;
        }
        if (b[l] < x) return l + 1;
        else return l;
    }

    public static void main(String[] args) throws IOException {
        n = nextInt();
        a = new int[n + 10];
        b = new int[n + 10];
        for (int i = 1; i <= n; ++i) {
            a[i] = nextInt();
        }
        Arrays.fill(b, 1000000000);
        int r = 1, l = 1;
        for (int i = 1; i <= n; ++i) {
            b[1] = Math.min(b[1], a[i]);
            int p = lower_bound(l, r, a[i]);
            if (p > r) {
                r = p;
                b[p] = a[i];
            } else b[p] = Math.min(b[p], a[i]);
        }
        out.println(r);
        out.flush();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值