Play with Digits(背包变形)
来源:互联网 发布:域名服务器ip地址 编辑:程序博客网 时间:2024/06/05 15:07
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.
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.
For each case, output the largest possible number Prof. Meng can create in one line. The answer should not contain any extra leading zeroes.
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
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;}
做了我好久。。智商压制
- Play with Digits(背包变形)
- 变形的背包问题
- HDU1114 完全背包变形
- poj3628(背包变形)
- hdu3033 分组背包变形
- poj 2184 背包变形
- 多重背包变形--poj1742
- 多重背包变形--poj2392
- hdu2955 01背包变形
- HDU2955 01 背包变形
- poj3211-01背包变形
- 01背包变形
- poj2184(01背包变形)
- hdu3033(分组背包变形)
- 01背包的变形
- 背包问题的变形
- 背包方案及变形
- 【01背包变形】 饭卡
- linux基础命令和知识(二)
- 五种常见的php设计模式
- 关于php中header()、setcookie()、session_start()函数错误的解决方法和解释
- iOS 推送
- 谈谈html转义字符
- Play with Digits(背包变形)
- 制作jks、pkcs12的keystore
- 磁盘配额 简要操作指南
- PL/SQL编程学习笔记
- 关于 HTML5 video全屏问题 的解答
- 安装xp centos 双系统
- Java实现HTML页面转PDF解决方案
- 用汇编语言编写程序打印ASCII表
- 不捡肥皂捡贝壳----00