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 104coins 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
(≤104, the total number of coins) and M
(≤102, 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 V1≤V2≤⋯≤Vk such that V1+V2+⋯+Vk=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
动态规划,0-1背包问题。
相当于背包的容量为m,共有n件物品,物品的体积和价值均为硬币的价值w[i],0-1背包问题主要是解决在不超过c的前提下,尽可能的让背包中硬币的总值更大的问题。而本题中的问题则是判断是否能填满整个背包,如果可以的话,取硬币价值序列最小的那个序列,即在我们在硬币的“总体积”不超过背包的“体积”的条件下,让硬币总价值尽可能的大,判断硬币的总价值能否达到背包的“体积”。
本题中取w[10010]记录每个(0-10010)硬币的价值兼体积;dp[110]代表背包在每一种体积(0-110)下能达到的最大容量;choice[10010][110]在背包体积为 j (0-110)的情况下是否选第 i (0-10010)个硬币。
状态转移方程为dp[j]=max{dp[j],dp[j-w[i]]+w[i]);
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int cmp(int a,int b){
return a>b;
}
int w[10010],dp[110];
bool choice[10010][110];
int n,m;
int main(){
scanf("%d %d",&n,&m);
for(int i=0;i<n;i++)
scanf("%d",&w[i]);
sort(w,w+n,cmp);
for(int i=0;i<n;i++){
for(int j=m;j>=w[i];j--){
if(dp[j]<=dp[j-w[i]]+w[i]){
choice[i][j]=true;
dp[j]=dp[j-w[i]]+w[i];
}
}
}
if(dp[m]!=m){
cout<<"No Solution";
return 0;
}
std::vector<int> result;
while(m>0){
while(choice[n][m]==false)
n--;
result.push_back(w[n]);
m-=w[n--];
}
for(int i=0;i<result.size();++i){
if(i!=0)
cout<<' ';
cout<<result[i];
}
}
递归解法
递归解的话,很容易超时,所以需要剪枝,如果已经得到一解,则flag为true,之后的都不用再算,但是这样最后一个测试点还是会超时,所以其次递归前判断一下,是否会有解,即coins 的总和是否大于等于target
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int n,target,sumcoin = 0;
bool flag = false;
vector<int>coins,ans,tempresult;
void search(int index,int sum){
if(flag == true ||sum > target)
return;
else if(sum == target){
flag = true;
ans = tempresult;
return;
}
for(int i = index; i < n; i++){
tempresult.push_back(coins[i]);
search(i+1,sum+coins[i]);
tempresult.pop_back();
}
}
int main(){
cin>>n>>target;
coins.resize(n);
for(int i = 0; i < n; i++){
scanf("%d",&coins[i]);
sumcoin += coins[i];
}
sort(coins.begin(), coins.end());
if(sumcoin >= target)
search(0,0);
if(flag == false)
printf("No Solution");
else{
printf("%d",ans[0]);
for(int i = 1; i < ans.size(); i++)
printf(" %d",ans[i]);
}
}