文章目录
- 1 题目
- 2 解析
- 2.1 题意
- 2.2 思路
- 3 参考代码
1 题目
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 9
5 9 8 7 2 3 4 1Sample Output 1:
1 3 5Sample Input 2:
4 8
7 2 4 3Sample Output 2:
No Solution
2 解析
2.1 题意
给n个价值的硬币,求这个n硬币中总价值恰为m的硬币序列(按价值从小到大排序),如果有多种方案,按字典需最小的输出
2.2 思路
0-1背包问题,价值c[i]和质量w[i]等价,
- 1 因为要按最小字典序输出,首先把输入的w[]数组按从大到小排序,让货币价值小的序号较大,
- 2 开一个二维数组choice[i][v](i表示货币的序号,v表示总价值),来记录dp[i][v]选择了那种策略,转移选择dp[i-1][v]状态,choice[i][v]=0(不放第i件物品),转移选择dp[i-1][v-w[i]]+w[i]状态,choice[i][v]=1(放第i件物品)
- 3 建立是否输出该货币的flag数组,求输出序列时,i从n开始递减枚举choice[i][v],如果choice==1,则v-w[i],flag[i] = true,继续循环,直到i小于0为止
- 4 i从n递减枚举,如果flag[i] == true,则输出
3 参考代码
#include <cstdio>
#include <algorithm>
#include <cstring>using std::sort;const int MAXN = 10010;
int w[MAXN];//钱币的价值
int dp[MAXN];
int choice[MAXN][MAXN];
bool flag[MAXN];bool cmp(int a, int b){return a > b;
}int main(int argc, char const *argv[])
{memset(flag, false, sizeof(flag));int n, m;scanf("%d%d", &n, &m);for (int i = 1; i <= n; ++i){scanf("%d", &w[i]);}sort(w + 1, w + n + 1, cmp);//从大到小排序,来满足价值从小到大的字典序顺序输出//数字小的占大序号for (int v = 0; v <= m; ++v){dp[v] = 0;}for (int i = 1; i <= n; ++i){for (int v = m; v >= w[i]; --v){int temp = dp[v - w[i]] + w[i];if(dp[v] <= temp){//选择放入第i件物件dp[v] = temp;choice[i][v] = 1;//选择货币价值大的沉底(序号较小)}else{choice[i][v] = 0;}}}if(dp[m] != m){//无解,不满足恰好能付清价值为m的货币printf("No Solution\n");}else{//记录最优路径int num = 0, index = n, v = m;//个数、序号(从大序号开始)、目前价值while(index >= 0){if(choice[index][v] == 1){//优先选择大序号的,即货币价值小的flag[index] = true;v -= w[index];num++;}else if(choice[index][v] == 0){flag[index] = false;}index--;}for (int i = n; i >= 0; --i){if(flag[i] == true){printf("%d", w[i]);num--;if(num > 0) printf(" ");}}}return 0;
}