Lanqiao Cup 2023 14th Provincial Competition Real Questions-Buy Melon-Java Question Solutions

Table of contents

Lanqiao Cup 2023 14th Provincial Competition Real Questions-Maigua

Question description

Input format

Output format

Sample input

Sample output

hint

[Analysis of ideas]

【Code】


Lanqiao Cup 2023 14th Provincial Competition Real Questions-Maigua

Time limit: 3s Memory limit: 320MB Submits: 796 Resolves: 69

Question description

Xiao Lan is buying melons at a melon stall. There are n melons on the melon stall, and the weight of each melon is Ai.

Little Blue Knife is so good that he can split any melon into two parts of exactly equal weight, but he can only chop each melon once.

Xiaolan hopes that the sum of the weights of the melons she wants to buy is exactly m.

Please ask Xiao Lan at least how many melons he has to split to buy a melon with a weight of exactly m. If Xiaolan cannot get a melon with a total weight of exactly m no matter what, please output −1.

Input format

The first line of input contains two integers n and m, separated by a space, which respectively represent the number of melons and the total weight of the melons that Xiaolan wants to buy.

The second line contains n integers Ai, separated by a space between adjacent integers, representing the weight of each melon respectively.

Output format

One line of output contains an integer representing the answer.

Sample input

copy

3 10
1 3 13

Sample output

copy

2

hint

For 20% of the evaluation use cases, ∑n≤10;

For 60% of the evaluation cases, ∑n≤20;

For all evaluation cases, 1 ≤ n ≤ 30, 1 ≤ Ai ≤ 109, 1 ≤ m ≤ 10^9

[Analysis of ideas]

This question is a very simple list of recursive possibilities, but there are three situations in each recursion, so the time complexity is O(3^N). The time complexity is too high, so it is necessary to get rid of those complete recursions during the recursion process. Impossible solutions reduce complexity.

【Code】

package LQB;

import java.util.Scanner;

/**
 * @ProjectName: study3
 * @FileName: Ex4
 * @author:HWJ
 * @Data: 2023/9/17 21:54
 */
public class Ex4 {
    static double[] subs; // subs[i]表示为西瓜i -西瓜n-1的西瓜质量和,用于对递归的降低可能性
    static double m;
    static int n;
    static int min = 40; // 因为n最大为30,所以最多劈瓜30次
    static double[] weights; // weights[i]表示为第i个西瓜的质量

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        n = input.nextInt();
        m = input.nextInt();
        weights = new double[n];
        subs = new double[n];
        for (int i = 0; i < n; i++) {
            weights[i] = input.nextInt();
        }
        subs[n - 1] = weights[n - 1];
        for (int i = n - 2; i >= 0; i--) {
            subs[i] = subs[i + 1] + weights[i];
        }
        int p = dfs(0, 0, 0);
        System.out.println(p == Integer.MAX_VALUE ? -1 : p);
    }

    // sum 表示现在搞定了多少西瓜   index 表示现在对第几个西瓜做决策   have表示现在已经劈了几次瓜了
    public static int dfs(double sum, int index, int have) {
        if (have >= min) { // 如果此时虽然满足要求但他大于了当前的最优情况,他不可能是最优解,直接排除掉
            return Integer.MAX_VALUE;
        }
        if (sum == m) { // 达到满足要求
            min = have; // 更新最小情况。
            return have;
        }
        if (sum > m) {
            return Integer.MAX_VALUE; // 此时不加任何西瓜 重量也已经超过了需要的重量,所以直接排除
        }
        if (index == n) {
            return Integer.MAX_VALUE; //此时已经使用了所有西瓜,也无法满足,直接排除掉
        }
        if (subs[index] + sum < m) {
            return Integer.MAX_VALUE; // 此时加上后面所有的西瓜也不满足条件,所以没有必要再递归了,
        }

        int p1 = dfs(sum + weights[index], index + 1, have);
        int p2 = dfs(sum + weights[index] / 2.0, index + 1, have + 1);
        int p3 = dfs(sum, index + 1, have);
        return Math.min(p1, Math.min(p2, p3));

    }

}

Guess you like

Origin blog.csdn.net/weixin_73936404/article/details/132956767