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 104 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
(≤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
题意: 给你一个数,和一个序列,找出一个最小子序列的和等于给定的数,若有按升序输出此序列,若无,则输出No Solution。
思路:刚开始一看这种题直接莽dfs,各种剪枝各种优化,最后一个节点还是JJ,出大问题!
果断放弃,开始莽DP,可以改一下DP中的01背包问题,把质量和价值合二为一为数的大小,把背包的容积定义为求和的大小,然后一顿乱莽,注意用visit数组来维护在第i次更新j容量时是否使用这个序列的第i个数,以此类推,归纳求出这个序列。
以下给出DP和DFS代码。
DP:
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
bool cmp(int a, int b){
return a > b;
}
int main(){
int n, m;
int dp[105] = {0};
bool visit[10005][105] = {false};
scanf("%d %d", &n, &m);
vector<int> vec(n + 1);
for(int i = 1; i <= n; i++){
scanf("%d", &vec[i]);
}
sort(vec.begin() + 1, vec.end(), cmp);
for(int i = 1; i <= n; i++){
for(int j = m; j >= vec[i]; j--){
if(dp[j] <= dp[j - vec[i]] + vec[i]){
visit[i][j] = true;
dp[j] = dp[j - vec[i]] + vec[i];
}
}
}
if(dp[m] == m){
vector<int> out_result;
int v = m;
int index = n;
while (v)
{
if(visit[index][v]){
out_result.push_back(vec[index]);
v -= vec[index];
}
index--;
}
for(int i = 0; i < out_result.size() - 1; i++){
printf("%d ", out_result[i]);
}
printf("%d\n", out_result[out_result.size() - 1]);
}else
{
printf("No Solution\n");
}
return 0;
}
DFS:
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int n;
int m;
vector<int> vec;
int visit[10001] = {0};
vector<int> temp_result;
vector<int> result;
int flag = false;
void dfs(int start_index, int sum){
if(sum == m && !flag){
result = temp_result;
flag = true;
return;
// if(!result.empty()){
// if(temp_result.size() < result.size()){
// for(int i = 0; i < temp_result.size(); i++){
// if(temp_result[i] < result[i]){
// result = temp_result;
// break;
// }
// if(temp_result[i] > result[i]){
// break;
// }
// }
// }else
// {
// for(int i = 0; i < result.size(); i++){
// if(temp_result[i] < result[i]){
// result = temp_result;
// break;
// }
// if(temp_result[i] > result[i]){
// break;
// }
// }
// }
// }else
// {
// result = temp_result;
// }
// return;
}
while (start_index < n && !flag)
{
if(visit[vec[start_index]] > 0 && sum + vec[start_index] <= m){
--visit[vec[start_index]];
temp_result.push_back(vec[start_index]);
dfs(start_index + 1, sum + vec[start_index]);
++visit[vec[start_index]];
temp_result.pop_back();
}else if(visit[vec[start_index]])
{
return;
}
++start_index;
}
}
int main(){
scanf("%d %d", &n, &m);
for(int i = 0; i < n; ++i){
int temp;
scanf("%d", &temp);
if(temp <= m){
vec.push_back(temp);
++visit[temp];
}
}
n = vec.size();
sort(vec.begin(), vec.end());
dfs(0, 0);
if(result.size()){
for(int i = 0; i < result.size() - 1; ++i){
printf("%d ", result[i]);
}
printf("%d\n", result[result.size() - 1]);
}else
{
printf("No Solution\n");
}
return 0;
}