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:**因为上一个题是升序,本题是降序。至于为什么要降序,这基于题目要求,它要求长度和宽度都必须降序。

相关推荐
plainGeekDev1 小时前
ButterKnife → ViewBinding
android·java·kotlin
像我这样帅的人丶你还17 小时前
Java 后端详解(四):分页与搜索
java·javascript·后端
她的男孩17 小时前
数据权限为什么不能只靠注解?Forge 的 Mapper 层 SQL 改写源码拆解
java·后端·架构
tntxia18 小时前
Mybatis的日志输入
java
亦暖筑序19 小时前
Java 8老系统Browser Agent实战:三层拦截把AI操作后台变成可审计流程
java·后端·设计模式
用户298698530141 天前
Java 实现 Word 文档加密与权限解除
java·后端
Yeats_Liao1 天前
14:Servlet中的页面跳转-Java Web
java·后端·架构
未秃头的程序猿1 天前
告别"if-else地狱"!Java 21模式匹配,代码优雅了10倍
java·后端·面试
鹤望兰6751 天前
字节跳动国际支付-后端开发-三面面经
java
Flittly1 天前
【AgentScope Java新手村系列】(14)人机交互
java·spring boot·spring