您的位置:首页 > 其它

PAT 1068. Find More Coins (30) DFS,01背包

2017-09-03 00:56 357 查看


1068. Find More Coins (30)

时间限制

150 ms

内存限制

65536 kB

代码长度限制

16000 B

判题程序

Standard

作者

CHEN, Yue

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


思路采用DFS。但是最后一个case怎么优化都过不了。。。最后发现,可以投机取巧过。。或者说这组数据就是让投机的?

也试出来case 5:sum==m

case 6:sum<m

#include <iostream>
#include<stdio.h>
#include<vector>
#include<set>
#include<algorithm>
using namespace std;

int a[10005];
int path[10005];
int sum=0;
int n,m;
int p=0;
int ok=0;
int toobig=0;
void dfs(int x,int aa)
{
//if(aa>10) return;    尝试剪枝,失败。
sum+=a[x];
path[p++]=a[x];
if(sum==m)
{
printf("%d",path[0]);
for(int i=1;i<p;i++)
printf(" %d",path[i]);
printf("\n");
sum-=a[x];
p--;
ok=1;
return;
}
if(sum>m)
{
p--;
sum-=a[x];
toobig=1;
return;
}
for(int i=x+1;i<n;i++)
{  if(ok==1) return;
if(toobig==1) break;
dfs(i,aa+1);
}
p--;
sum-=a[x];
toobig=0;
}

int main()
{
cin>>n>>m;
int ssmm=0;
for(int i=0;i<n;i++)
{
int tmp;
scanf("%d",&tmp);
ssmm+=tmp;
a[i]=tmp;
}
sort(a,a+n);
if(ssmm<m) {cout<<"No Solution";      //最后一个case靠这个过的。。
return 0;}
//  if(ssmm==m)
//  {
//      cout<<a[0];
//      for(int i=1;i<n;i++)
//          printf(" %d",&a[i]);
//   }

for(int i=0;i<n;i++)
{  if(ok==1) break;
p=sum=0;
dfs(i,0);
}
if(ok==0) cout<<"No Solution";
return 0;
}
还有一种01背包的做法。记录路径这个方法吊炸天。
可以把硬币看成w=v(即容量=价值)的物品,现在要选取这些物品放入到容量为m的背包中,求能装的最大价值。
如果最大价值恰好等于容量m,那么方案则是可行的,否则输出No Solution。
由于要输出排列最小的方案,所以先将硬币按价值从大到小排列,相当于我先装大的,再装小的.值得注意的是变量初始化的问题。如果要恰好装满,只能让dp[0][x]=-INF,而dp[x][0]都为0才行。


#include <iostream>
#include<stdio.h>
#include<vector>
#include<set>
#include<algorithm>
using namespace std;

int a[10005];
int   path[10000]={0};
int n,m;
int dp[10001][101]={0};
int mark[10001][101]={0};

bool cmp (int x,int y)
{
return x>y;
}

int main()
{
cin>>n>>m;
int ssmm=0;
for(int i=1;i<=n;i++)
{
int tmp;
scanf("%d",&tmp);
ssmm+=tmp;
a[i]=tmp;
}
sort(a+1,a+1+n,cmp);

//  for(int i=1;i<m;i++)            这句注释加上就变成恰好装满问题,同样通过
//   dp[0][i]=-999;

for(int i=1;i<=n;i++)
for(int j=a[i];j<=m;j++)
{
dp[i][j]=max(dp[i-1][j-a[i]]+a[i],dp[i-1][j]);
if(dp[i-1][j-a[i]]+a[i]>=dp[i-1][j])
mark[i][j]=1;
}

if(dp
[m]!=m) cout<<"No Solution";
else
{

int p=0;
for(int i=n;i>=1;i--)
if(mark[i][m])
{
path[p++]=a[i];
m-=a[i];
}
cout<<path[0];
for(int i=1;i<p;i++)
cout<<' '<<path[i];
}
return 0;
}





                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: