FATE
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3957 Accepted Submission(s): 1751
Problem Description
最近xhd正在玩一款叫做FATE的游戏,为了得到极品装备,xhd在不停的杀怪做任务。久而久之xhd开始对杀怪产生的厌恶感,但又不得不通过杀怪来升完这最后一级。现在的问题是,xhd升掉最后一级还需n的经验值,xhd还留有m的忍耐度,每杀一个怪xhd会得到相应的经验,并减掉相应的忍耐度。当忍耐度降到0或者0以下时,xhd就不会玩这游戏。xhd还说了他最多只杀s只怪。请问他能升掉这最后一级吗?
Input
输入数据有多组,对于每组数据第一行输入n,m,k,s(0 < n,m,k,s < 100)四个正整数。分别表示还需的经验值,保留的忍耐度,怪的种数和最多的杀怪数。接下来输入k行数据。每行数据输入两个正整数a,b(0 < a,b < 20);分别表示杀掉一只这种怪xhd会得到的经验值和会减掉的忍耐度。(每种怪都有无数个)
Output
输出升完这级还能保留的最大忍耐度,如果无法升完这级输出-1。
Sample Input
10 10 1 10 1 1 10 10 1 9 1 1 9 10 2 10 1 1 2 2
Sample Output
0 -1 1package hd; import java.util.Scanner; /** * @功能Function Description: * @开发环境Environment: eclipse * @技术特点Technique: 动态规划(背包) * @版本Version: Scanner * @作者Author: follow your dreams * @日期Date: 20120822 * @备注Notes: * @链接: http://acm.hdu.edu.cn/showproblem.php?pid=2159 */ public class HD2159_1_20120822 { public static int[] w;//保存杀每种怪所消耗的忍耐值 public static int[] v;//保存杀每种怪得到的经验值 public static int[][] dp; public static void main(String[] args) { Scanner sc = new Scanner(System.in); int maxx, maxy, n, countCase; while(sc.hasNextInt()) { maxx = sc.nextInt();//最大经验值 maxy = sc.nextInt();//最大忍耐度 n = sc.nextInt(); countCase = sc.nextInt();//怪的个数 v = new int[n+1];//经验值 w = new int[n+1];//消耗值 for(int i=1; i<=n; i++) { v[i] = sc.nextInt(); w[i] = sc.nextInt(); } dp = new int[countCase+1][maxy+1]; dp(n, maxy, countCase); //show(dp); System.out.println(getResult(dp, maxx, maxy)); } } public static int max(int x, int y) { return x>y ? x : y; } /** * * @param n 怪兽种类 * @param maxx 最大经验值 * @param maxy 最大忍耐度 * @param countCase 怪的个数 */ public static void dp(int n, int maxy, int countCase) { for(int x=1; x<=countCase; x++) {//杀y只怪兽,最大的价值 for(int y=1; y<=maxy; y++) { for(int i=1; i<=n; i++) {//怪兽的种类 if(y-w[i] >= 0) { dp[x][y] = max(dp[x][y], dp[x-1][y-w[i]]+v[i]); } } } } } /** * 从第0列开始查找(按照列开始查找),如果某列的值>=需要的经验值得花,那么要消耗的最小忍耐度就是列号(列号就是当前消耗的忍耐度) * @param dp * @param maxx 至少要达到的经验值 * @param maxy 最大忍耐度 * @return */ public static int getResult(int[][] dp, int maxx, int maxy) { int row = dp.length; int col = dp[0].length; for(int j=0; j<col; j++) { for(int i=0; i<row; i++) { if(dp[i][j] >= maxx) { return maxy - j; } } } return -1;// } // public static void show(int[][] arr) { // for(int i=0; i<arr.length; i++) { // for(int j=0; j<arr[0].length; j++) { // System.out.print(arr[i][j] + " "); // } // System.out.println(); // } // System.out.println(); // } }