P1616 疯狂的采药(DP,完全背包,洛谷,java)

洛谷链接:https://www.luogu.com.cn/problem/P1616
用二维数组炸内存了!!!!!!!

在这里插入图片描述
在这里插入图片描述

import java.util.Scanner;
public class Main{
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int T=in.nextInt();             
		int M=in.nextInt();             
		int[] v=new int[10001];           
		int[] t=new int[10001];           
		int[] f=new int[100001];
		
		for(int i=1;i<=M;i++) {
			t[i]=in.nextInt();
			v[i]=in.nextInt();
		}
		
		for(int i=1;i<=M;i++) {
			for(int j=t[i];j<=T;j++) {  //当需要的左边的值为数组越界时,值不变
		        f[j]=Math.max(f[j],f[j-t[i]]+v[i]);  //递推
			}
		}
		System.out.println(f[T]);
	}
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值