Java 动态规划 P1441 砝码称重

题目地址
在这里插入图片描述

不会做借鉴了一下大佬的题解…
题解

先用DFS中枚举要去掉的砝码和已经选择过的砝码,然后用01背包记录称得砝码的最大值。

import java.util.*;

public class Main {

	private static int n, m, re = 0;
	private static int arr[], dp[], f[];
	
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		n = scan.nextInt();
		m = scan.nextInt();
		arr = new int[n + 1];
		for (int i = 0; i < n; i++) {
			arr[i] = scan.nextInt();
		}

		f = new int[20021];//记录砝码有没有被去掉 有被选中去掉为1,否则为0
		DFS(0, 0);
		System.out.println(re);
	}

	public static void DFS(int cur, int now) {
		// cur:当前已经选择过的砝码 now:当前已选择放弃的砝码
		if (now > m) {
			return;
		}
		if (cur == n) {
			if (now == m) {
				Cal();
			}
			return;
		}
		// 不放弃当前砝码
		DFS(cur + 1, now);
		f[cur] = 1;
		// 放弃当前砝码
		DFS(cur + 1, now + 1);
		f[cur] = 0;

	}

	public static void Cal() {
		dp = new int[20021];//可能会调用多次,需要初始化
		dp[0] = 1;//没选择任何砝码,结果只有1种
		int tot = 0, ans = 0;
		for (int i = 0; i < n; i++) {
			if (f[i] == 1)//已经选择去掉的砝码就跳过
				continue;
			for (int j = tot; j >= 0; j--) {//倒着算保证每个砝码只选一次
				if (dp[j] == 1 && dp[j + arr[i]] == 0) {
					//这边的+,指的是加上新砝码进行称重
					dp[j + arr[i]] = 1;
					ans++;
				}
			}
			tot += arr[i];
		}
		re = Math.max(ans, re);
	}
}

(小菜鸟第一次发题解,如果有思路不清晰的地方,欢迎指正

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值