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 1Sample Output 1:
1 3 5Sample Input 2:
4 8 7 2 4 3Sample Output 2:
No Solution
思路分析:可以用dfs + 剪枝搜索结果,最好的方法是动态规划(01背包)。这两种方法都能AC
dfs就是枚举每一个硬币,拿或不拿的状态,O(T) = O(2^N),必须要加剪枝。另外加了剪枝可能还AC不了,因为最后一个测试点比较极端,M大于序列的和,所以是无解,在读入数据的时候记录所有硬币的和,小于给定的M就是无解了。(不加这个最后一个测试点超时)
经典的01背包,需要判断有无解,并回溯打印路径。注意用二维DP数组会内存超限,这里要用优化的滚动数组,是一维的。
先上DFS代码:
#include <cstdio>
#include <algorithm>
#include <vector>
#define MAX 10000
using namespace std;
int a[MAX];
vector<int> temp;
vector<int> vec;
bool flag = false;
void dfs( int curIndex, int curSum, int n, int m ) {
if( flag ) return;
if( curSum == m ) {
vec = temp;
flag = true;
return;
}
else {
for( int i = curIndex; i < n; i++ ) {
if( curSum + a[i] <= m ) {
temp.push_back( a[i] );
dfs( i + 1, curSum + a[i], n, m );
temp.pop_back();
}
else {
return;
}
}
}
}
void dfs( int curIndex, int need, int n ) {
/*
for( int i = 0; i < temp.size(); i++ ) {
printf( "%d ", temp[i] );
}
printf( "need: %d", need );
printf( "\n" );
*/
if( need == 0 ) {
vec = temp;
flag = true;
return;
}
for( int i = curIndex; i < n; i++ ) {
if( need - a[i] < a[i] && need != a[i] ) continue;
//printf( "选择%d\n", a[i] );
temp.push_back( a[i] );
dfs( i + 1, need - a[i], n );
if( flag ) return;
temp.pop_back();
}
}
int main() {
//freopen( "123.txt", "r", stdin );
int n, m;
scanf( "%d%d", &n, &m );
int sum = 0;
for( int i = 0; i < n; i++ ) {
scanf( "%d", &a[i] );
sum += a[i];
}
if( m > sum ) {
printf( "No Solution\n" );
return 0;
}
sort( a, a + n );
//dfs( 0, 0, n, m );
dfs( 0, m, n );
if( !flag ) {
printf( "No Solution\n" );
}
else {
for( int i = 0; i < vec.size(); i++ ) {
if( i == 0 ) printf( "%d", vec[i] );
else printf( " %d", vec[i] );
}
}
return 0;
}
下面是01背包:
#include <cstdio>
#include <algorithm>
#include <vector>
#define MAX 10000 + 10
using namespace std;
int w[MAX];
int dp[MAX];
int path[MAX][MAX];
vector<int> vec;
int cmp( int a, int b ) {
return a > b ? 1 : 0;
}
int main() {
int n, m;
scanf( "%d%d", &n, &m );
for( int i = 1; i <= n; i++ ) {
scanf( "%d", &w[i] );
}
sort( w + 1, w + 1 + n, cmp );
for( int i = 1; i <= n; i++ ) {
for( int v = m; v >= 0; v-- ) {
if( v - w[i] >= 0 ) {
if( dp[v - w[i]] + w[i] >= dp[v] ) {
dp[v] = dp[v - w[i]] + w[i];
path[i][v] = 1;
}
else {
dp[v] = dp[v];
path[i][v] = 0;
}
}
else {
dp[v] = dp[v];
path[i][v] = 0;
}
}
}
if( dp[m] != m ) {
printf( "No Solution\n" );
return 0;
}
int k = n;
int v = m;
while( k >= 0 ) {
if( path[k][v] == 1 ) {
vec.push_back( w[k] );
v = v - w[k];
}
k--;
}
sort( vec.begin(), vec.end() );
for( int i = 0; i < vec.size(); i++ ) {
if( i == 0 ) printf( "%d", vec[i] );
else printf( " %d", vec[i] );
}
return 0;
}