PAT-A-1068. Find More Coins (30)
来源:互联网 发布:4g网络优化工程师 编辑:程序博客网 时间:2024/05/17 22:44
1068. Find More Coins (30)
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 95 9 8 7 2 3 4 1Sample Output 1:
1 3 5Sample Input 2:
4 87 2 4 3Sample Output 2:
No Solution
#include<iostream>#include<cstdio>#include<algorithm>using namespace std;const int maxn = 100010;const int maxv = 110;int w[maxn];int dp[maxv] = { 0 };bool ch[maxn][maxv], flag[maxn];int cmp(int a, int b){ return a > b;}int main(){ int n, m; cin >> n >> m; for (int i = 1; i <= n; i++) cin >> w[i]; sort(w + 1, w + 1 + n, cmp); for (int i = 1; i <= n; i++) { for (int v = m; v >= w[i]; v--) { if (dp[v] <= dp[v - w[i]] + w[i]) { dp[v] = dp[v - w[i]] + w[i]; ch[i][v] = 1; } else { ch[i][v] = 0; } } } if (dp[m] != m) cout << "No Solution" << endl; else { int k = n, num = 0, v = m; while (k >= 0) { if (ch[k][v] == 1) { flag[k] = true; v = v - w[k]; num++; } else flag[k] = false; k--; } for (int i = n; i >= 1; i--) { if (flag[i] == true) { cout << w[i]; num--; if (num > 0) cout << " "; } } } system("pause"); return 0;}
- PAT A 1068. Find More Coins (30)
- PAT(A) - 1068. Find More Coins (30)
- PAT-A-1068. Find More Coins (30)
- pat 1068. Find More Coins (30)
- PAT 1068. Find More Coins (30)
- 【PAT】1068. Find More Coins (30)
- pat 1068. Find More Coins (30)
- PAT甲级1068. Find More Coins (30)
- pat 1068. Find More Coins (30)
- 【PAT甲级】1068. Find More Coins (30)
- PAT 甲级 1068. Find More Coins (30)
- PAT 1068. Find More Coins
- PAT 1068. Find More Coins
- 【PAT】1068. Find More Coins
- PAT--1068. Find More Coins
- PAT 1068. Find More Coins
- 【PAT Advanced Level】1068. Find More Coins (30)
- PAT 1068. Find More Coins (30)(背包问题,动态规划)
- X字斩技能触发器
- Javascript高级程序设计 Ch2
- 自定义软件安装界面(c#)
- XML 追加节点 带缩进
- 打印99乘法口诀表
- PAT-A-1068. Find More Coins (30)
- 设计模式(十六)原型模式
- vc串口通讯
- 代码文件的编码问题
- ARP协议及脚本
- MD5 加密
- mybatispse 插件 【 推荐eclipse 下 从mybatis 接口直接跳转到 xml 的插件】
- 判断并打印100~200间素数
- 【剑指offer】面试题 9:用两个栈实现队列