Play with Digits(背包变形)
2013-12-12 01:10
211 查看
Description
Prof. Meng likes playing with digits. He finds a store selling digits made from some expensive metal. Different kinds of digits may be sold at different prices, and we may assume that the amount of each kind of digits is unlimited.
Prof. Meng wants to find out the largest number he can create using no more than his total budget of M units of money. Could you help him?
For example, the store sells three kinds of digits, 0, 1, and 2, at prices of 5, 7, and 8 units of money respectively. If Prof. Meng has 14 units of money, the largest number he can create is 20, which costs him 13 (8 + 5) units of money.
Input
The input begins with a line containing an integer T (T<=80), which indicates the number of test cases.
Each case has two lines. The first line contains 10 integers in one line. The ith integer represents price of digit i – 1. Each price is between 1 and 50. -1 denotes that the corresponding digit is not on sale. The second line contains
an integer M (1<=M<=50), indicating Prof. Meng’s total budget. It is guaranteed that Prof. Meng can buy at least one digit.
Output
For each case, output the largest possible number Prof. Meng can create in one line. The answer should not contain any extra leading zeroes.
Sample Input
Copy sample input to clipboard
Sample Output
做了我好久。。智商压制
Prof. Meng likes playing with digits. He finds a store selling digits made from some expensive metal. Different kinds of digits may be sold at different prices, and we may assume that the amount of each kind of digits is unlimited.
Prof. Meng wants to find out the largest number he can create using no more than his total budget of M units of money. Could you help him?
For example, the store sells three kinds of digits, 0, 1, and 2, at prices of 5, 7, and 8 units of money respectively. If Prof. Meng has 14 units of money, the largest number he can create is 20, which costs him 13 (8 + 5) units of money.
Input
The input begins with a line containing an integer T (T<=80), which indicates the number of test cases.
Each case has two lines. The first line contains 10 integers in one line. The ith integer represents price of digit i – 1. Each price is between 1 and 50. -1 denotes that the corresponding digit is not on sale. The second line contains
an integer M (1<=M<=50), indicating Prof. Meng’s total budget. It is guaranteed that Prof. Meng can buy at least one digit.
Output
For each case, output the largest possible number Prof. Meng can create in one line. The answer should not contain any extra leading zeroes.
Sample Input
Copy sample input to clipboard
3 5 7 8 -1 -1 -1 -1 -1 -1 -1 14 1 2 3 4 5 6 7 8 9 10 1 1 1 1 1 -1 1 -1 1 1 1 3
Sample Output
20 0 999
#include <iostream> #include <cstdio> #include <cmath> #include <cstring> #include <string> using namespace std; int bud; const int maxn=1000000000; string go(string x,int j) { int length=x.size(); if (length==1 && x[0]=='a') { x[0]=j+'0'; return x; } x.resize(length+1); x[length]=j+'0'; return x; } bool judge(string a,string b,int pos) { if (b[b.size()-1]=='0' && pos==bud) return true; if (b.size()==1 && b[0]=='a') return true; if (a.size()>b.size()) return true; if (a.size()<b.size()) return false; else { int i; for (i=a.size()-1;i>=0;i--) if (a[i]==b[i]) continue; else if (a[i]>b[i]) return true; else return false; return false; } } int main() { int T; scanf("%d",&T); while (T--) { string dp[105]; int i; int A[10]; for (i=0;i<=100;i++) dp[i]="a"; for (i=0;i<=9;i++) scanf("%d",&A[i]); scanf("%d",&bud); int j; for (j=0;j<=9;j++) { for (i=1;i<=bud;i++) { if (A[j]!=-1 && i>=A[j] ) { string a=go(dp[i-A[j]],j); //cout << "a " << a << endl; if (judge(a,dp[i],i)) dp[i]=a; }//printf("%d ",dp[i]); //cout << dp[i] << ' '; } //printf("\n"); } if (dp[bud][dp[bud].size()-1]=='0') printf("0\n"); else { for (i=dp[bud].size()-1;i>=0;i--) printf("%c",dp[bud][i]); printf("\n"); } } return 0; }
做了我好久。。智商压制
相关文章推荐
- UVA12563Jin Ge Jin Qu hao(01背包变形:两个条件最优化)
- leetcode Count Numbers with Unique Digits
- leecode_357 Count Numbers with Unique Digits
- UVA 10673 Play with Floor and Ceil
- LeetCode Count Numbers with Unique Digits(计数问题)
- Count Numbers with Unique Digits——Difficulty:Medium
- 01背包的变形
- SOJ 3664 chess towers 【完全背包变形】
- HDU-2159 FATE,01背包变形
- ACM 350. 小吃店(背包变形)
- poj 3046 Ant Counting (DP多重背包变形)
- LeetCode[357] Count Numbers with Unique Digits
- uva 10673 Play with Floor and Ceil
- HDU 1114 Piggy-Bank 完全背包变形
- 算法练习3:Playing with digits
- Play-With-Docker在chrome上的插件
- HDOJ 3487 Play with Chain
- 01 背包 变形之转移价值
- hdu 2189 悼念512汶川大地震遇难同胞——来生一起走(完全 背包变形--求方案总数)
- 【BZOJ4355】Play with Sequence