地址:http://acm.nyist.net/JudgeOnline/problem.php?pid=16
题目:
矩形嵌套
时间限制:3000 ms | 内存限制:65535 KB
难度:4
-
描述
-
有n个矩形,每个矩形可以用a,b来描述,表示长和宽。矩形X(a,b)可以嵌套在矩形Y(c,d)中当且仅当a<c,b<d或者b<c,a<d(相当于旋转X90度)。例如(1,5)可以嵌套在(6,2)内,但不能嵌套在(3,4)中。你的任务是选出尽可能多的矩形排成一行,使得除最后一个外,每一个矩形都可以嵌套在下一个矩形内。
-
输入
-
第一行是一个正正数N(0<N<10),表示测试数据组数,
每组测试数据的第一行是一个正正数n,表示该组测试数据中含有矩形的个数(n<=1000)
随后的n行,每行有两个数a,b(0<a,b<100),表示矩形的长和宽
输出
- 每组测试数据都输出一个数,表示最多符合条件的矩形数目,每组输出占一行 样例输入
-
1 10 1 2 2 4 5 8 6 10 7 9 3 1 5 8 12 10 9 7 2 2
样例输出
-
5
-
第一行是一个正正数N(0<N<10),表示测试数据组数,
思路:首先先理解题目,尽可能多的将小的矩形嵌套大的矩形里面。
思路一:一开始想到,先按长进行排列,找到最大的,然后从小到大将小的嵌套里面,这时分两种情况:一种是小的长<最大的宽,另一种是小的长>最大的宽。
public void MaxNested(Node[] nodes, int length) {
int remainA = nodes[length - 1].a;
int remainB = nodes[length - 1].b;
int top = 0;
while (top < length - 1 && nodes[top].b < remainB) { // 长,宽符合要求
if (nodes[top].a < remainB&&nodes[top].b<remainA) // 长《最大的宽情况,宽<剩下的长
remainA -= nodes[top].b;
else if(nodes[top].a > remainB&&nodes[top].a<remainA)// 长》最大的宽情况,长<剩下的长
remainA -= nodes[top].a;
else
break;
top++;
}
System.out.println(top);
}
结国给出的样例都能通过,问题是这组不能:
1 200
2 300
3 4
5 6
7 8
只想到第一步,却没有想到第二部,没考虑到这种极限条件,取到最大2 300这组矩形,却装不下任何的矩形,实际是可以装下3个的。
这时就出现了一个问题,不能单单靠长,这个条件去判断来决定最大的,宽也必须考虑进行,这样就有两个变量需要考虑和解决了。。就不知道怎么解答。
思路二:逛尽讨论区才发现,这道题是动态规划中最长递增子序列的问题变形。但刚开始还是想不通,怎么是递增子序列了。最直接的方法就是进行模拟了。参考下列代码
for(i = 0; i < n; i++){
dp[i] = 1;
for(j = 0; j < i; j++){
if(G[j].y < G[i].y && G[j].x < G[i].x && dp[i] < dp[j] + 1)
dp[i] = dp[j] + 1;
}
}
当模拟到i=3的时候,你就会发现,真的是最长递增子序列的问题,分情况为1个矩形,会有几个嵌套, 1个矩形,会有几个嵌套,2个矩形,会有几个嵌套,2个矩形,
会有几个嵌套,.......... 每多一个都建立在前一个情况的基础上。就是动态规划的问题:
//动态规划,取最优,很多时候,要依靠两个变量求最优的时候,就要用上动态规划,因为排序后,单方面贪心,碰到极限情况就出不来了
//42 677
public void MaxNestedDp(Node[] nodes,int length){
int[] dp=new int[length];
for(int i=0;i<length;i++){
dp[i]=1; //初始化,怎么样都是会有一个的
for(int j=0;j<i;j++){
//一个,两个,三个,....的情况的嵌套相当于求最长递增子序列
//因为有两个变量长,宽,不能靠一方面单单的排序就解决,有特殊情况(2 300,3 4 5 6 7 8)
//要讨论全局,取最好的情况,就用用到动态规划
if(nodes[j].a<nodes[i].a&&nodes[j].b<nodes[i].b&&dp[i]<dp[j]+1)
dp[i]=dp[j]+1;
}
}
//选出最大
int max=0;
for(int i=0;i<length;i++){
if(dp[i]>max)
max=dp[i];
}
System.out.println(max);
}
最后附上完整代码:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Main main = new Main();
Scanner cin = new Scanner(System.in);
int n = cin.nextInt();
while (n-- > 0) {
int length = cin.nextInt();
Node[] nodes = new Node[length];
// 插入排序
int tmpA, tmpB, tmp;
for (int i = 0; i < length; i++) {
tmpA = cin.nextInt();
tmpB = cin.nextInt();
if (tmpB > tmpA) {
tmp = tmpB;
tmpB = tmpA;
tmpA = tmp;
}
int j = i;
nodes[j] = main.new Node();
while (j > 0 && tmpA < nodes[j - 1].a) {
nodes[j].a = nodes[j - 1].a;
nodes[j].b = nodes[j - 1].b;
j--;
}
nodes[j].a = tmpA;
nodes[j].b = tmpB;
}
main.MaxNestedDp(nodes, length);
//输出排序
/*for (int i = 0; i < length; i++) {
System.out.print(nodes[i].a + " " + nodes[i].b + ",");
if (i % 5 == 0)
System.out.println();
}*/
}
}
/*
* 1 200
2 300
3 4
5 6
7 8
这组数据就无法通过了
*/
public void MaxNested(Node[] nodes, int length) {
int remainA = nodes[length - 1].a;
int remainB = nodes[length - 1].b;
int top = 0;
while (top < length - 1 && nodes[top].b < remainB) { // 长,宽符合要求
if (nodes[top].a < remainB&&nodes[top].b<remainA) // 长《最大的宽情况,宽<剩下的长
remainA -= nodes[top].b;
else if(nodes[top].a > remainB&&nodes[top].a<remainA)// 长》最大的宽情况,长<剩下的长
remainA -= nodes[top].a;
else
break;
top++;
}
System.out.println(top);
}
//动态规划,取最优,很多时候,要依靠两个变量求最优的时候,就要用上动态规划,因为排序后,单方面贪心,碰到极限情况就出不来了
//42 677
public void MaxNestedDp(Node[] nodes,int length){
int[] dp=new int[length];
for(int i=0;i<length;i++){
dp[i]=1; //初始化,怎么样都是会有一个的
for(int j=0;j<i;j++){
//一个,两个,三个,....的情况的嵌套相当于求最长递增子序列
//因为有两个变量长,宽,不能靠一方面单单的排序就解决,有特殊情况(2 300,3 4 5 6 7 8)
//要讨论全局,取最好的情况,就用用到动态规划
if(nodes[j].a<nodes[i].a&&nodes[j].b<nodes[i].b&&dp[i]<dp[j]+1)
dp[i]=dp[j]+1;
}
}
//选出最大
int max=0;
for(int i=0;i<length;i++){
if(dp[i]>max)
max=dp[i];
}
System.out.println(max);
}
class Node {
int a; // 长
int b; // 宽
}
}