uva 103 - Stacking Boxes
2013-04-20 12:31
337 查看
import java.io.*; import java.util.*; class box implements Comparable<box>{ int dimention; LinkedList<Integer> arr; int orginal; double vol; public box(int d,Scanner scan,int t){ dimention = d; orginal = t; arr = new LinkedList<Integer>(); for(int i=0;i<d;i++){ int tmp = scan.nextInt(); arr.add(tmp); vol += Math.log(tmp); } Collections.sort(arr); } @Override public int compareTo(box o) { return ((Double) vol).compareTo(o.vol); } public boolean Small_than(box t){ for(int i=0;i<this.dimention;i++){ if(this.arr.get(i)>=t.arr.get(i)) return false; } return true; } } public class Main { public static void backTrack(box[] b,int[] pre,int point,int level){ if(point==-1){ return; } backTrack(b,pre,pre[point],level+1); if(level!=0) System.out.print(b[point].orginal+1+" "); else{ System.out.println(b[point].orginal+1); } } public static void main(String[] args) throws Exception { Scanner scan = new Scanner(System.in); while(scan.hasNextInt()){ int n = scan.nextInt(); int size = scan.nextInt(); box b[] = new box ; int pre[] = new int ; Arrays.fill(pre, -1); for(int i=0;i<n;i++){ b[i] = new box(size,scan,i); } Arrays.sort(b); int dp[] = new int[n*size]; Arrays.fill(dp, 1); int max = 0;int last = -1; for(int i=1;i<n;i++){ for(int j=0;j<i;j++){ if(b[j].Small_than(b[i])){ if(dp[j]+1>dp[i]){ dp[i] = dp[j]+1; pre[i] = j; } } } if(dp[i]>max){ max = dp[i]; last = i; } } System.out.println(max); backTrack(b,pre,last,0); } } };
这个题是LIS 这个没啥好说的 太明显了。
犯了几个错误
1. 以为输入的box旋转的时候必须保留原来顺序,也就是说 5 8 9 4 => 4 5 8 9 => 9 4 5 8
我跑去拿全排列做,其实不是, 读入进去直接sort 就行,可以乱序
2. 这个问题做LIS必须排序,不然像1 2 3 4 5 6这样的盒子不在最底下还怎么做LIS?
开始拿容积做排序,发现用Long貌似也WA, 看网上有人求对数做的。。。蛋疼
这个类似那种3-D box stacking的问题,那个因为有高,所以不是简单对base排序就行,
那个题目是直接读入一个Box然后插入3种盒子,比如读入 1,2,3 插入 1,2,3; 2,1,3; 3,1,2
Base的长宽就无所谓了,在后面dp的时候判断。
还是水啊,这个题目有思路又A不过去。。。
附点数据,过了就过了
22 3 44 71 27 37 91 79 96 10 74 52 35 19 40 98 85 67 79 43 93 29 13 37 87 17 75 24 74 36 31 11 89 80 84 75 31 39 20 88 87 14 89 70 25 30 93 28 20 72 11 30 78 87 36 50 54 72 21 20 28 57 68 50 25 80 45 58 5 2 35 45 72 49 74 75 65 84 96 44 29 1 54 13 74 42 93 72 65 37 20 97 13 77 43 93 17 44 38 54 48 98 63 40 85 81 92 62 72 96 58 26 6 65 41 49 98 87 92 13 26 17 30 75 65 72 36 59 28 63 69 56 11 47 95 77 84 81 98 69 31 87 60 27 14 52 68 32 68 29 97 92 13 48 95 19 25 53 38 79 14 84 38 62 58 33 17 32 83 76 48 81 23 33 79 96 52 56 28 10 79 55 18 15 91 39 69 97 98 48 14 52 23 79 18 39 72 43 68 88 93 15 55 10 89 44 81 50 58 12 74 39 22 25 63 65 39 94 59 63 16 44 28 89 72 52 80 76 53 80 42 77 35 91 33 78 14 77 20 22 14 99 98 35 55 12 30 26 83 12 75 83 45 64 23 66 85 46 10 88 27 19 58 83 73 51 89 15 93 15 6 82 64 50 42 36 40 35 39 58 63 47 29 57 77 49 37 67 73 59 13 62 88 29 57 10 99 50 30 17 93 80 55 74 79 14 45 46 40 55 98 91 13 72 87 27 88 98 89 33 32 12 30 86 45 45 51 88 77 92 20 50 12 42 88 35 87 62 24 69 71 77 51 21 15 58 48 71 31 65 86 36 26 46 93 97 64 27 49 16 58 29 5 23 30 83 49 59 20 43 93 75 61 33 72 97 74 13 46 26 43 75 38 82 30 76 15 88 15 82 46 11 77 59 98 62 58 52 27 76 65 23 73 51 29 83 59 15 81 72 41 32 58 66 10 37 98 49 72 71 43 59 18 19 60 68 95 50 70 26 84 56 45 81 37 45 56 42 82 96 96 49 71 81 94 78 69 69 78 15 55 87 89 19 35 43 37 42 46 98 22 12 99 99 30 90 60 18 44 56 16 94 95 59 57 25 64 74 99 18 85 77 35 38 99 83 58 25 86 52 41 60 63 50 35 60 10 25 18 69 45 14 86 50 12 58 94 10 27 2 39 26 63 17 64 46 75 13 26 25 21 45 15 22 41 41 98 92 27 81 37 65 39 25 53 50 72 55 12 42 66 65 10 96 90 90 93 77 24 70 64 49 87 79 33 99 59 11 49 43 43 31 76 85 27 5 35 20 10 91 64 77 51 94 40 72 81 32 58 39 36 18 95 49 79 77 87 62 61 95 85 48 87 56 64 47 53 98 32 20 24 39 47 95 59 33 40 30 95 90 68 69 81 90 39 95 43 75 56 19 23 27 24 99 86 80 10 20 29 84 57 57 98 98 99 18 48 17 94 66 20 85 34 47 41 20 66 40 15 59 71 83 40 25 40 36 58 88 96 74 98 94 85 65 50 54 93 79 43 62 17 42 82 38 57 76 53 17 73 98 12 40 92 48 86 61 18 21 89 73 39 72 72 39 96 33 31 58 51 90 65 16 1 36 58 18 15 37 70 72 25 88 52 73 39 26 38 56 17 9 9 80 94 76 27 45 67 60 87 46 94 87 22 65 51 72 58 41 68 86 30 13 64 45 41 66 37 63 32 50 94 82 93 80 43 95 32 11 27 16 86 92 39 36 12 17 87 14 10 46 88 79 21 61 51 37 74 41 71 41 96 46 95 81 94 88 28 81 46 49 26 65 45
83 72 37 95 26 84 56 94 35
结果:
6
10 4 19 9 2 5
3
1 2 4
25
2 15 9 8 17 22 4 13 16 19 1 29 26 21 7 6 3 12 24 23 25 5 28 10 20
4
2 14 10 5
3
13 15 8
5
19 4 15 26 16
11
7 5 1 26 25 13 14 27 22 18 9
4
13 25 24 5
16
4 16 3 8 13 1 5 14 12 10 15 2 6 7 11 9
2
5 8
相关文章推荐
- UVa 103 Stacking Boxes --- DAG上的动态规划
- 【DP】UVA 103 Stacking Boxes 输出路径
- UVA 103 Stacking Boxes(DAG 上的最长路及其字典序输出)
- uva 103 - Stacking Boxes 动态规划——最长路及字典序
- UVa Problem 103 - Stacking Boxes
- uva103-Stacking_Boxes(dp最长递增子序列)
- UVA 103 Stacking Boxes 套箱子 DAG最长路 dp记忆化搜索
- uva103 - Stacking Boxes(动归,记忆化搜索)
- 【DP】UVA 103 Stacking Boxes 输出路径
- UVa 103 Stacking Boxes 解题源码
- Stacking Boxes - UVa 103 dp
- uva 103 Stacking Boxes(最长上升子序列)
- [动态规划]UVA103 - Stacking Boxes
- uva 103 - Stacking Boxes DAG最长路
- uva 103 Stacking Boxes
- UVA 103--- Stacking Boxes
- uva 103 (dp)Stacking Boxes
- UVA 103 Stacking Boxes --LIS
- Uva 103-Stacking Boxes(DP/矩形嵌套)
- UVA - 103 Stacking Boxes(DAG最长路)