• 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. }

  • 相关阅读:
    流媒体传输 - HLS 协议
    Blender DreamUV插件使用简明教程
    冶金行业S2B2B商城交易系统:夯实企业资源聚合能力,提升管理水平
    理德外汇:美联储9月如期暂停加息,暗示年内或再加息一次
    Linux下 gdb 调试打印进程内存信息
    交互设计师必须知道的五大交互设计流程
    JavaWeb三大组件之Servlet------Servlet详细讲解
    【教程】AWD中如何通过Python批量快速管理服务器?
    合并单元格
    如何正确的清理C盘
  • 原文地址:https://blog.csdn.net/weixin_53199925/article/details/126167464