点击题号跳转

A5985 B1792 C5644 D4038 E3229

F3322 G3614 H2824 I4286 J6225

A.矩形嵌套回到顶部

题意

n个矩形嵌套,跟E题类似,求最多嵌套,n<=1000

题解

这个题因为比较的是点对,所以可以先排序,使得第一维有序,然后只需要求出第二维的最长上升子序列,复杂度O(nlogn)

代码

 1 import java.util.*;
 2 
 3 public class Main {
 4 
 5     public static void main(String[] args) {
 6 
 7         Solution solver = new Solution();
 8         solver.solve();
 9     }
10 
11 }
12 
13 class Solution {
14 
15     private final int N = 1005;
16 
17     private int[] dp = new int[N << 1];
18     private List<Node> list = new ArrayList<>();
19 
20     class Node {
21         int l, r;
22         Node(int l, int r) {
23             this.l = l;
24             this.r = r;
25         }
26     }
27 
28     public void solve() {
29         Scanner sc = new Scanner(System.in);
30         int t = sc.nextInt();
31         while (sc.hasNext()) {
32             int n = sc.nextInt();
33             list.clear();
34             for (int i = 0; i < n; i++) {
35                 int l = sc.nextInt();
36                 int r = sc.nextInt();
37                 list.add(new Node(l, r));
38                 list.add(new Node(r, l));
39                 dp[i] = dp[n + i] = 0;
40             }
41 
42             Collections.sort(list, (o1, o2) -> {
43                 if (o1.l > o2.l) return 1;
44                 else if (o1.l == o2.l) {
45                     if (o1.r > o2.r) return 1;
46                     else if (o1.r == o2.r) return 0;
47                 }
48                 return -1;
49             });
50 
51             //list.stream().forEach((node) -> System.out.println(node.l + " " + node.r));
52 
53             int maxx = 0;
54             for (int i = 0; i < n << 1; i++) {
55                 dp[i] = 1;
56                 for (int j = 0; j < i; j++) {
57                     if (list.get(i).l > list.get(j).l && list.get(i).r > list.get(j).r) {
58                         dp[i] = Math.max(dp[i], dp[j] + 1);
59                     }
60                 }
61                 maxx = Math.max(maxx, dp[i]);
62             }
63 
64             System.out.println(maxx);
65         }
66     }
67 }
A

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-11-01
  • 2021-12-24
  • 2022-01-04
  • 2021-12-01
  • 2021-07-17
  • 2021-09-16
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案