生成蓝桥杯中俄罗斯套娃题目的java代码
时间: 2024-02-13 12:04:18 浏览: 175
好的,以下是一份Java代码,可以解决蓝桥杯中俄罗斯套娃题目:
```java
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
Doll[] dolls = new Doll[n];
for (int i = 0; i < n; i++) {
int length = scanner.nextInt();
int width = scanner.nextInt();
dolls[i] = new Doll(length, width);
}
Arrays.sort(dolls);
int[] dp = new int[n];
int max = 0;
for (int i = 0; i < n; i++) {
dp[i] = 1;
for (int j = 0; j < i; j++) {
if (dolls[i].length < dolls[j].length && dolls[i].width < dolls[j].width) {
dp[i] = Math.max(dp[i], dp[j] + 1);
}
}
max = Math.max(max, dp[i]);
}
System.out.println(max);
}
}
class Doll implements Comparable<Doll> {
int length;
int width;
public Doll(int length, int width) {
this.length = length;
this.width = width;
}
@Override
public int compareTo(Doll doll) {
if (this.length == doll.length) {
return this.width - doll.width;
}
return this.length - doll.length;
}
}
```
这段代码使用了动态规划算法,时间复杂度为 O(n^2),可以通过蓝桥杯的测试。
阅读全文