1068. Find More Coins (30)-PAT甲级真题(01背包)_柳婼的博客-CSDN博客
柳婼的解法看不太懂,先贴一下....
根据她的版本,我简化了一下(用二维的dp,更好理解一些,但空间复杂度会增加),写了自己的dp版本(dp[i][j]表示前i个硬币中选择,总额小于等于j的最大总额,choice[i][j]表示相应时候是否选择硬币i):
#include <cstdio>
#include <vector>
#include <algorithm>
const int MAXN = 10001;
const int MAXM = 101;
int N, M, sum;
int dp[MAXN][MAXM];
bool choice[MAXN][MAXM];
std::vector<int> value;
bool cmp(const int &a, const int &b){
return a > b;
}
int main(){
scanf("%d %d", &N, &M);
value.resize(N + 1);
for(int i = 1; i <= N; ++i){
scanf("%d", &value[i]);
}
sort(value.begin() + 1, value.end(), cmp);
for(int i = 1; i <= N; ++i){
for(int j = 0; j <= M; ++j){
if(j >= value[i] && dp[i - 1][j] <= dp[i - 1][j - value[i]] + value[i]){
choice[i][j] = true;
dp[i][j] = dp[i - 1][j - value[i]] + value[i];
} else{
dp[i][j] = dp[i - 1][j];
}
}
}
if(dp[N][M] != M){
printf("No Solution");
} else{
std::vector<int> res;
int i = N;
int j = M;
while(j != 0){
if(choice[i][j]){
res.push_back(value[i]);
j -= value[i];
}
i--;
}
for(i = 0; i < res.size(); ++i){
printf("%d%s", res[i], i == res.size() - 1 ? "" : " ");
}
}
return 0;
}
我自己的做法如下,但这个解法复杂度比较高,当数据比较复杂的时候应该是超时的...
#include <cstdio>
#include <algorithm>
#include <vector>
int N, M, sum;
bool flag = false;
std::vector<int> value, curr;
void dfs(int n){
if(sum == M){
for(int i = 0; i < curr.size(); ++i){
printf("%d%s", curr[i], i == curr.size() - 1 ? "" : " ");
}
flag = true;
return;
}
if(sum + value[n] > M || n == N){
return;
}
curr.push_back(value[n]);
sum += value[n];
dfs(n + 1);
if(flag){
return;
}
curr.pop_back();
sum -= value[n];
dfs(n + 1);
}
int main(){
scanf("%d %d", &N, &M);
value.resize(N);
sum = 0;
for(int i = 0; i < N; ++i){
scanf("%d", &value[i]);
sum += value[i];
}
if(sum < M){
printf("No Solution");
return 0;
}
sort(value.begin(), value.end());
if(sum == M){
for(int i = 0; i < value.size(); ++i){
printf("%d%s", value[i], i == value.size() - 1 ? "" : " ");
}
return 0;
}
sum = 0;
dfs(0);
if(!flag){
printf("No Solution");
}
return 0;
}
题目如下:
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≥1such 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