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 (≤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
思路
我的做法是DFS,感觉这题*PAT甲级1103 Integer Factorization//DFS很像,也算参考了这题的做法,同时参考了PAT-A 1068. Find More Coins (30) DP和DFS两种方法实现的DFS,写出了这个没超时版的DFS。
思路就是先将数字从小到大排序,然后对数组进行DFS,找出来的第一组数据就是字典序最小的数据
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int N, M, threshold;
bool vis[10001], finded = false;
vector<int> record, ans, temp;
void DFS(int index, int sum);
int main()
{
cin >> N >> M;
int total = 0;
for (int i = 0; i < N; i++)
{
int num;
cin >> num;
total += num;
record.push_back(num);
}
if (total < M)
cout << "No Solution" << endl;
else
{
sort(record.begin(), record.end());
threshold = N - 1; //总数相加要等于M,首先筛掉大于M的
for (int i = 0; i < N; i++)
{
if (record[i] >= M)
{
threshold = i;
break;
}
}
DFS(0, 0);
if (finded)
{
for (int i = 0; i < ans.size(); i++)
{
if (i != 0)
cout << " ";
cout << ans[i];
}
}
else
cout << "No Solution" << endl;
}
system("pause");
return 0;
}
void DFS(int index, int sum)
{
if (finded || sum > M) //如果找到了或者总数大于M则返回
return;
if (sum == M) //总数等于M表示找到了
{
finded = true;
ans = temp;
return;
}
for (int i = 0; i <= threshold; i++)
{
if (!vis[i])
{
vis[i] = true;
temp.push_back(record[i]);
DFS(i, sum + record[i]);
vis[i] = false;
temp.pop_back();
}
}
}
法二——01背包问题
算法笔记中的做法
while (k >= 0)
{
if (choice[k][v] == 1)
{
flag[k] = true;
v -= w[k];
num++;
}
else
flag[k] = false;
k--;
}
#include <iostream>
#include <algorithm>
#include <cstdlib>
using namespace std;
enum {NotSet = 0, Set};
const int maxn = 10010, maxv = 110;
int N, M;
int w[maxn], dp[maxv];
bool choice[maxn][maxv], flag[maxn];
bool cmp(int a, int b) { return a > b; }
int main()
{
cin >> N >> M;
for (int i = 0; i < N; i++)
cin >> w[i];
sort(w, w + N, cmp);
for (int i = 0; i < N; i++)
{
for (int v = M; v >= w[i]; v--)
{
if (dp[v] <= dp[v - w[i]] + w[i])
{
dp[v] = dp[v - w[i]] + w[i];
choice[i][v] = true;
}
else
choice[i][v] = false;
}
}
if (dp[M] != M)
cout << "No Solution" << endl;
else
{
int k = N - 1, num = 0, v = M;
while (k >= 0)
{
if (choice[k][v] == 1)
{
flag[k] = Set;
v -= w[k];
num++;
}
else
flag[k] = NotSet;
k--;
}
for (int i = N - 1; i >= 0; i--)
{
if (flag[i] == true)
{
cout << w[i];
num--;
if (num > 0)
cout << " ";
}
}
}
system("pause");
return 0;
}