编码-京东实习笔试编程题-生日礼物-动态规划

来源:互联网 发布:广州网站seo 编辑:程序博客网 时间:2024/05/13 07:21

题目:

生日礼物 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Problem Description: BF的生日快到了,这一次,小东决定为BF送一份特别的生日礼物为其庆生。作为高智商中的佼佼者,BF在国外求学,因此小东无法与之一起庆生。小东计划送一个生日卡片,并通过特别的包装让BF永远难忘。

她决定把卡片套装在一系列的信封A = {a1,  a2,  …,  an}中。小东已经从商店中购买了很多的信封,她希望能够用手头中尽可能多的信封包装卡片。为防止卡片或信封被损坏,只有长宽较小的信封能够装入大些的信封,同尺寸的信封不能套装,卡片和信封都不能折叠。

小东计算了邮寄的时间,发现她的时间已经不够了,为此找你帮忙包装,你能帮她吗? 输入 输入有若干组,每组的第一行包含三个整数n, w, h,1<=n<=5000, 1<=w, h<=10^6,分别表示小东手头的信封数量和卡片的大小。紧随其后的n行中,每行有两个整数wi和hi,为第i个信封的大小,1<=wi, hi<=10^6。 输出 对每组测试数据,结果第一行中输出最多能够使用的信封数量,结果第二行中按使用顺序输出信封的编号。由于小东有洁癖,她对排在前面的信封比较有好感,若有多个信封可用,她喜欢用最先拿到的信封。另外别忘了,小东要求把卡片装入能够装的最小信封中。 如果卡片无法装入任何信封中,则在单独的行中输出0。

样例输入 2 1 1 2 2 2 2 3 3 3 5 4 12 11 9 8​ 样例输出 1 1 3 1 3 2

解题思路:
具体思路如图片所示,首先针对所有的数据进行排序,排序的依据是信封的面积,然后利用动态规划的备忘录法,依次向前寻找其能存放的列,并将该数据与那一列组成新的一列(当前方多个列满足要求,并且前方列的高度相同时,取最前面的),最后输出的结果就是最优解。
这里写图片描述

详细的代码如下所示:(本人采用了Java来解,不过思路差不多,各位可以自由尝试)

import java.util.ArrayList;import java.util.Arrays;import java.util.Comparator;import java.util.List;import java.util.Scanner;public class GiftProblem {    public static void main(String[] args) {        Scanner scanner = new Scanner(System.in);        while(scanner.hasNextInt()){            int num = scanner.nextInt();            int w = scanner.nextInt();            int h = scanner.nextInt();            int[][] temps = new int[num][4];            int nums = 0;            for (int i = 0; i < num; i++) {                int tempWidth=scanner.nextInt();                int tempHigh=scanner.nextInt();                if (tempWidth>w && tempHigh>h) {                    temps[nums][0] = tempWidth;                    temps[nums][1] = tempHigh;                    temps[nums][2] = i+1;                    temps[nums][3] = temps[nums][0]*temps[nums][1];                    nums++;                }            }            findTemp(Arrays.copyOfRange(temps, 0, nums),nums,w,h);        }    }    public static void findTemp(int[][] temps, int num, int w, int h) {        //首先对所有的数据进行排序,以面积的形式进行排序        Arrays.sort(temps,new Comparator<int[]>() {            @Override            public int compare(int[] o1, int[] o2) {                if (o1[3]>o2[3]) return 1;                if (o1[3]<o2[3]) return -1;                return 0;            }        });        List<List<int[]>> resultList = new ArrayList<>();        List<int[]> headList = new ArrayList<>();        headList.add(new int[]{w,h,1});        resultList.add(headList);        for (int i = 1; i <= num; i++) {            int levelNow = 0;            for (int j = i-1; j >= 0; j--) {                List<int[]> list = resultList.get(j);                int oldLevel = list.size();                if(oldLevel<levelNow-1) break;                int[] oldTop = list.get(oldLevel-1);                if (oldTop[0]<temps[i-1][0] && oldTop[1]<temps[i-1][1]) {                    List<int[]> tempList = new ArrayList<>();                    tempList.addAll(list);                    tempList.add(temps[i-1]);                    //更新resultList,保存最优值                    if (resultList.size()==i+1) resultList.remove(i);                    resultList.add(tempList);                    levelNow = oldLevel+1;                }            }        }        System.out.println("最多可以包裹的信封数:\t"+(resultList.get(num).size()-1));        for (int i = 1; i < resultList.get(num).size(); i++) {            System.out.print(resultList.get(num).get(i)[2]+" ");        }    }}

转载请注明本博客地址,多谢。

0 0