pat1068. Find More Coins (30)

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

提交代码

 

 

学习网址:http://blog.csdn.net/xyt8023y/article/details/47255241

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<iostream>
 4 #include<stack>
 5 #include<set>
 6 #include<map>
 7 #include<queue>
 8 #include<algorithm>
 9 using namespace std;
10 int mo[10005],f[10005][105];
11 bool vis[10005][105];
12 bool cmp(int a,int b)
13 {
14     return a>b;
15 }
16 int main()
17 {
18     //freopen("D:\\INPUT.txt","r",stdin);
19     int i,j,n,m;
20     scanf("%d %d",&n,&m);
21     for(i=1; i<=n; i++)
22     {
23         scanf("%d",&mo[i]);
24     }
25     sort(mo+1,mo+n+1,cmp);
26     //f[i][j]=maz(f[i-1][j],f[i-1][j-mo[i]]+mo[i])
27     for(i=1; i<=n; i++)
28     {
29         for(j=1; j<=m; j++)
30         {
31             if(mo[i]<=j&&(f[i-1][j-mo[i]]+mo[i]>=f[i-1][j]))//这里要取到等号,为了状态的衔接
32             {
33                 f[i][j]=f[i-1][j-mo[i]]+mo[i];
34                 vis[i][j]=true;
35             }
36             else
37             {
38                 f[i][j]=f[i-1][j];
39             }
40         }
41     }
42     /*for(i=1;i<=n;i++){
43         for(j=1;j<=m;j++){
44             cout<<i<<" "<<j<<" "<<f[i][j]<<endl;
45         }
46         cout<<endl;
47     }*/
48 
49     if(f[n][m]!=m)
50     {
51         printf("No Solution\n");
52     }
53     else
54     {
55         vector<int> v;
56         while(m)
57         {
58             while(!vis[n][m])
59             {
60                 n--;
61             }
62             v.push_back(mo[n]);
63             m-=mo[n];
64             n--;
65         }
66         printf("%d",v[0]);
67         for(i=1;i<v.size();i++){
68             printf(" %d",v[i]);
69         }
70         printf("\n");
71     }
72     return 0;
73 }

 

posted @ 2015-09-10 09:51  Deribs4  阅读(318)  评论(0编辑  收藏  举报