PAT 1068 Find More Coins 01揹包

Eva loves to collect coins from all over the universe, including some other planets like Mars. One day she visited a universal shopping mall which could accept all kinds of coins as payments. However, there was a special requirement of the payment: for each bill, she must pay the exact amount. Since she has as many as 10​4​​ coins with her, she definitely needs your help. You are supposed to tell her, for any given amount of money, whether or not she can find some coins to pay for it.

Input Specification:

Each input file contains one test case. For each case, the first line contains 2 positive numbers: N (≤10​4​​, the total number of coins) and M (≤10​2​​, the amount of money Eva has to pay). The second line contains N face values of the coins, which are all positive numbers. All the numbers in a line are separated by a space.

Output Specification:

For each test case, print in one line the face values V​1​​≤V​2​​≤⋯≤V​k​​ such that V​1​​+V​2​​+⋯+V​k​​=M. All the numbers must be separated by a space, and there must be no extra space at the end of the line. If such a solution is not unique, output the smallest sequence. If there is no solution, output "No Solution" instead.

Note: sequence {A[1], A[2], ...} is said to be "smaller" than sequence {B[1], B[2], ...} if there exists k≥1 such that A[i]=B[i] for all i<k, and A[k] < B[k].

Sample Input 1:

8 9
5 9 8 7 2 3 4 1

Sample Output 1:

1 3 5

Sample Input 2:

4 8
7 2 4 3

Sample Output 2:

No Solution

题意:给出N枚硬币价值,要求找出面值和恰好为M的硬币序列,若有多个解则给出字典序最小的解。

按照01揹包问题求解:把硬币看成是w=v(质量=价值)的物品,选取一部分物品塞满容量为M的揹包,使得总价值最大,若最大价值等于M则满足题意,否则无解。其中质量和价值等价,只需要一个数组coins[i]表示即可。题目要求从小到大输出coin组合,且多解时输出字典序最小的解,因此需要将数组从大到小排序,依次选择硬币考虑是否放入揹包,当加入硬币后价值>=原价值时,将该硬币放入揹包(等号是因为当出现相同的价值时,选取当前硬币后的字典序一定比之前的小),并用数组selected[i][v]=1记录选取该硬币。dp数组求解完成后,利用selected数组从第n个硬币开始倒着找出所有被选择的硬币。

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;

public class Main {
	
	static int maxn=10010,maxv=110;
	static int[] dp=new int[maxv];
	static Integer[] coins=new Integer[maxn];
	static boolean[][] selected=new boolean[maxn][maxv];
	
    public static void main(String[] args) throws IOException {
    	BufferedReader reader=new BufferedReader(new InputStreamReader(System.in));

    	String[] line=reader.readLine().split(" ");
    	int N=Integer.valueOf(line[0]),M=Integer.valueOf(line[1]);
    	line=reader.readLine().split(" ");
    	for(int i=1;i<=N;i++) {
    		coins[i]=Integer.valueOf(line[i-1]);
    	}
    	//降序排列
    	Arrays.sort(coins, 1, N+1, Collections.reverseOrder());
    	for(int i=1;i<=N;i++) {
    		for(int v=M;v>=coins[i];v--) {
    			if(dp[v]<=dp[v-coins[i]]+coins[i]) {//选择该硬币后价值更大
    				dp[v]=dp[v-coins[i]]+coins[i];//更新
    				selected[i][v]=true;//选择第i个硬币
    			}else {//不选
    				selected[i][v]=false;
    			}
    		}
    	}
    	if(dp[M]!=M)//最大价值不满足条件
    		System.out.println("No Solution");
    	else {
    		//查找最优路径
    		int k=N,v=M;
    		while(k>=0) {//反向查找
				if(selected[k][v]) {
    				System.out.print(coins[k]);
					v-=coins[k];//剩余价值
					if(v!=0)
    					System.out.print(" ");
						
				}
    			k--;
    		}
    	}
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章