去哪Q2通过80%,背包问题、分支限界、求解

如题。改半天改不出来直接交了。
上次携程笔试赛码网读输入有毒,这次特意加了一堆检查条件结果依然80%
package test.QUnaer;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

public class Main {
    public static int max;
    public static int current_s = 0;
    public static int best = 0;
    public static Queue<HeapNode> queue;

    static class HeapNode {
        int level;
        int seat;
        int maxbound;

        public HeapNode(int level, int maxbound, int seat) {
            this.level = level;
            this.maxbound = maxbound;
            this.seat = seat;
        }
    }

    public static void main(String[] args) {
//        int num = 5;
//        int seats = 100;
//        int[] a = {10,20,30,40,50};


        Scanner sc = new Scanner(System.in);
        String ipt = sc.nextLine();
        int num = Integer.valueOf(ipt.split(" ")[0]);
        int seats = Integer.valueOf(ipt.split(" ")[1]);
        max = seats;
        String[] bagg = sc.nextLine().split(" ");
        int[] a = new int[bagg.length];
        for (int i = 0; i < bagg.length; i++) {
            a[i] = Integer.valueOf(bagg[i]);
        }
        Arrays.sort(a);
        queue = new LinkedList<>();
        if(seats!=0)
            allocate(num,seats,a);
        else {
            if (Arrays.asList(a).contains(0) || num == 0)
                System.out.println("perfect");
            else
                System.out.println("good");
        }

    }

    public static int maxBound(int level, int[] bag ) {
        int left = max - current_s;
        int bound = current_s;
        while (level <= bag.length-1 && left >= bag[level]) {
            left -= bag[level];
            bound+=bag[level];
            level++;
        }
        return bound;
    }

    public static void addCurrentLiveNode (int level,int maxbound, int seat) {
        HeapNode heapNode = new Main.HeapNode(level,maxbound,seat);
        queue.add(heapNode);
    }

    public static void allocate(int num, int seats, int[] bag ) {
        int i = 0;
        int maxbound = maxBound(0,bag);
        while (i!=num) {
            int st = current_s + bag[i];
            if (st == seats) {
                best = st;
                break;
            }

            if (st < max) {
                best = current_s+bag[i]>best?current_s+bag[i]:best;
                addCurrentLiveNode(i+1,maxbound,st);
            }
            maxbound = maxBound(i+1,bag);
            if (best <= maxbound) {
                addCurrentLiveNode(i+1,maxbound,current_s);
            }
            if (queue.isEmpty())
                break;
            HeapNode heapNode = queue.poll();
            current_s = heapNode.seat;
            maxbound = heapNode.maxbound;
            i = heapNode.level;
        }
        if (best == max)
            System.out.println("perfect");
        else
            System.out.println("good");
    }

}

#春招##笔试题目##去哪儿##实习#
全部评论
第一题直接输出7就有20%,我等菜鸡只能这样拿分了
点赞 回复 分享
发布于 2018-04-02 17:07
这一看就是卡常数了,判断下sum是否小于m,如果小于直接就good,这样80%就变成AC了。
点赞 回复 分享
发布于 2018-04-02 17:17
q1你ac了么?我60不知道哪错
点赞 回复 分享
发布于 2018-04-02 17:05

相关推荐

03-11 21:46
西北大学 Java
河和静子:这只是实习工资,我学长北大通班博一的,他同学被这家天天发邮件让他去实习,一个月10w
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务