Play with Digits(背包变形)

来源:互联网 发布:域名服务器ip地址 编辑:程序博客网 时间:2024/06/05 15:07
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
35 7 8 -1 -1 -1 -1 -1 -1 -1141 2 3 4 5 6 7 8 9 1011 1 1 1 -1 1 -1 1 1 13
Sample Output
200999


#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;elsereturn 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;}

做了我好久。。智商压制


0 0
原创粉丝点击