POJ 3624 A
来源:互联网 发布:mysql count 性能优化 编辑:程序博客网 时间:2024/05/19 00:09
简单01背包http://poj.org/problem?id=3624
注意空间超限问题:
1、可以使用一维数组来优化
2、使用滚动数组
//使用一维数组import java.util.Scanner;import java.util.concurrent.locks.Condition;import java.util.concurrent.locks.ReentrantLock;/** * Created by hms on 2017/3/28. */public class Solution { public static void main(String[] args) { int N, M; Scanner scanner = new Scanner(System.in); N = scanner.nextInt(); M = scanner.nextInt(); int w[] = new int[N+5]; int v[] = new int[N+5]; for(int i = 1; i <= N; ++i) { w[i] = scanner.nextInt(); v[i] = scanner.nextInt(); } int dp[] = new int[M+5]; for(int i = 1; i <= N; ++i) { for(int j = M; j >= w[i]; --j) { dp[j] = Math.max(dp[j], dp[j-w[i]] + v[i]); } } System.out.println(dp[M]); }}
//使用滚动数组import java.util.Scanner;import java.util.concurrent.locks.Condition;import java.util.concurrent.locks.ReentrantLock;/** * Created by hms on 2017/3/28. */public class Main { public static void main(String[] args) { int N, M; Scanner scanner = new Scanner(System.in); N = scanner.nextInt(); M = scanner.nextInt(); int w[] = new int[N+5]; int v[] = new int[N+5]; for(int i = 1; i <= N; ++i) { w[i] = scanner.nextInt(); v[i] = scanner.nextInt(); } int dp[][] = new int[2][M+5]; for(int i = 1; i <= N; ++i) { for(int j = M; j >= 0; --j) { if(j >= w[i]) dp[i&1][j] = Math.max(dp[(i-1)&1][j] , dp[(i-1)&1][j-w[i]]+v[i]); else dp[i&1][j] = dp[(i-1)&1][j]; } } System.out.println(dp[N&1][M]); }}
0 0
- POJ 3624 A
- POJ 2449 A*+SPFA
- POJ 1077 Eight A*
- POJ 2243 A*算法
- POJ 2449 A*+spfa
- poj Whac-a-Mole
- POJ 2243 A* 算法
- poj A Funny Game
- POJ A Chess Game
- poj 1000 a+b
- poj 2243(a*)
- poj 1001 a+b
- POJ 1376 Robot A* -
- poj 1576 A/B
- poj 2449 A*
- Find a multiple POJ
- Find a multiple (POJ
- POJ 1286 A
- Android 反编译教程
- Driver Test
- IDEA、PyCharm激活服务器地址
- 对象数组和对象指针
- HDU 6030 Happy Necklace[找规律][矩阵快速幂]
- POJ 3624 A
- java.util.UnknownFormatConversionException: Conversion = ',' 解决
- ConcurrentHashMap
- Keras-数据集介绍
- PhpStorm 快捷键大全 PhpStorm 常用快捷键和配置
- PHP学习
- 帧动画功能 纯js控制css代码
- leetcode-candy
- JavaScript基础之对象