Java中的进阶最长上升子序列——LIS

Java中的进阶LIS-260326

嘻嘻我又来了,写完这个就睡觉,我感觉我已经学会了,啊哈哈哈哈!这个没法用语言讲,直接上例题吧。

例题1(FROM 洛谷 P2782)

思路

将南岸北岸想象成两条线,现在有南岸的城市 a1,a2 (a1<a2),在北岸有两个城市 b1,b2,如果 b1>b2 那么航线相交,反之则不相交,具体化可以看下图。所以我们知道,如果我们将南岸城市按升序排列,再求北岸对应的序列中最长上升子序列的长度。

代码实现

这里用到了内部类进行二维数组的排序,想学习可以去看 -> Java中的内部类

java 复制代码
import java.util.*;
import java.io.*;

public class Main{
    static BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
    static StringTokenizer st;
    public static void main(String [] args) throws IOException{
        int n = Integer.parseInt(next());
        int [][] city  = new int [n][2];
        for(int i=0;i<n;i++){
            city[i][0] = Integer.parseInt(next());
            city[i][1] = Integer.parseInt(next());
        }
        Arrays.sort(city,(a,b)->Integer.compare(a[0],b[0]));//内部类
        int [] north = new int[n];
        for(int i=0;i<n;i++)
            north[i] = city[i][1];
        List<Integer> t = new ArrayList<>();
        for(int x:north){
            int idx = Collections.binarySearch(t,x);
            if(idx < 0)
                idx = -(idx+1);
            if(idx == t.size())
                t.add(idx,x);
            else t.set(idx,x);
        }
        System.out.print(t.size());
    }
    static String  next() throws IOException{
        while(st==null || !st.hasMoreTokens()){
            String str = bf.readLine();
            if(str == null) return null;
            st = new StringTokenizer(str);
        }
        return st.nextToken();
    }
}

例题2(FROM 洛谷 P)

思路

其实就是要求这个序列中有几个不下降子序列,我们前面基础篇讲过Dilworth 定理 ,想学习的可以去看 -> Java中的最长上升子序列和非递增子序列------LNIS 和 LIS 。Dilworth 定理:在任意有限偏序集(Partially Ordered Set, poset)中,最大反链的大小 = 最小链划分的数量。在本题里就是:最长上升子序列的长度 = 下降子序列数量。

代码实现

java 复制代码
import java.util.*;
public class Main{
    
    public static void main(String [] args){
        Scanner input = new Scanner (System.in);
        int n = input.nextInt();
        int [][] wood = new int [n][2];
        for(int i=0;i<n;i++){
            wood[i][0] = input.nextInt();//l
            wood[i][1] = input.nextInt();//w
        }
        Arrays.sort(wood,(a,b)->{
            if(a[0] != b[0])
                return Integer.compare(b[0],a[0]);//如果长度不一样,按长度降序排序
        return Integer.compare(b[1],a[1]);});//如果长度一样,按宽度降序排序
        int [] w = new int [n];
        for(int i=0;i<n;i++)
            w[i] = wood[i][1];
        List<Integer>t = new ArrayList<>();
        for(int x : w){
            int idx = Collections.binarySearch(t,x);
            if(idx<0)
                idx = -(idx+1);
            if(idx == t.size())
                t.add(idx,x);
            else t.set(idx,x);
        }
        System.out.print(t.size());
    }
}

可能会有的问题:
Q: 为什么本题里使用定理要排序,而基础篇的例题里却没用排序?
A: 这是因为本题中不依赖原始序列的顺序,也就是说,我们可以任意排列这个序列,题里并未设限。而基础版里的那个题,它严格的要求了必须是子序列,依赖原始序列顺序,所以不能排序。

**Q:**为什么在上一题使用内部类的时候,是a0在前,而本题是b0在前

**A:**因为上一个题是升序,本题是降序。至于为什么要降序,这基于题目要求,它要求长度和宽度都必须降序。

相关推荐
CoderIsArt7 小时前
C#中UI 线程与 Dispatcher
开发语言·ui·c#
微尘寒风7 小时前
【Git】的安装和使用
java·git
y = xⁿ7 小时前
DeepSeek Harness 学习日记:关于Agent接口,工具调用的底层实现
android·java·学习
侧耳倾听1118 小时前
jwt使用简介
java·jwt
顶点多余8 小时前
那些在算法中适合巩固的知识点---1
java·前端·算法
AI人工智能+电脑小能手9 小时前
大白话说Java设计模式-46-责任链模式(业务实战篇)
java·设计模式·责任链模式·风控校验·servlet filter·spring interceptor·链式处理
jay神9 小时前
【计算机毕业设计】基于SpringBoot的程序教学辅助系统
java·前端·vue.js·spring boot·后端·毕业设计·课程设计
AI情绪识别开源9 小时前
检信 ALLEMOTION OS 加密打包可执行程序 — 全面测试报告版本: v1.3功能测试 / 性能测试 /
开发语言·数据结构·人工智能·功能测试
2601_9620664910 小时前
【Sql Server】Update中的From语句,以及常见更新操作方式
android·java·数据库