• 1068 Find More Coins


    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

    Sample Input 1:

    1. 8 9
    2. 5 9 8 7 2 3 4 1

    Sample Output 1:

    1 3 5
    

    Sample Input 2:

    1. 4 8
    2. 7 2 4 3

    Sample Output 2:

    No Solution

    一开始使用 dfs+剪枝 测试点6超时了,特判一下所有硬币加起来也小于m的情况就可以过了:

    1. #include
    2. #include
    3. using namespace std;
    4. int n, m, a[10010], ans[10010], cnt, sum, total;
    5. bool vis[10010], flag;
    6. void dfs(int x) {
    7. if (flag) {
    8. return;
    9. }
    10. if (sum == m) {
    11. flag = 1;
    12. for (int i = 0; i < cnt; i++) {
    13. printf("%d", ans[i]);
    14. if (i != cnt - 1) {
    15. printf(" ");
    16. }
    17. }
    18. return;
    19. }
    20. if (x >= n || sum > m) {//剪枝
    21. return;//无解
    22. }
    23. if (sum + a[x] <= m && (!vis[x])) { //有选或者不选两种
    24. vis[x] = 1;
    25. sum += a[x];
    26. int t = cnt;
    27. ans[cnt++] = a[x];
    28. dfs(x + 1);
    29. cnt = t;
    30. sum -= a[x];
    31. vis[x] = 0;
    32. dfs(x + 1);
    33. } else { //只有不选这一种
    34. dfs(x + 1);
    35. }
    36. }
    37. int main() {
    38. scanf("%d %d", &n, &m);
    39. for (int i = 0; i < n; i++) {
    40. scanf("%d", &a[i]);
    41. total += a[i];
    42. }
    43. if (total < m) {
    44. printf("No Solution");
    45. return 0;
    46. }
    47. sort(a, a + n);
    48. dfs(0);
    49. if (!flag) {
    50. printf("No Solution");
    51. }
    52. return 0;
    53. }

  • 相关阅读:
    web前端 html+css+javascript游戏网页设计实例 (网页制作课作业)
    备战蓝桥杯---图论应用1
    DirectX11 With Windows SDK--39 阴影技术(VSM、ESM)
    不同的二叉搜索树
    支持向量机之线性可分向量机
    windows 环境下,编译android 版opencv-4.5.5,并添加opencv_contrib-4.5.5 扩展模块
    浏览器基本原理
    arm ubuntu 换源
    六大设计原则
    java计算机毕业设计志愿者社会服务管理系统源码+mysql数据库+系统+lw文档+部署
  • 原文地址:https://blog.csdn.net/weixin_53199925/article/details/126167464