最大报销额
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 11181 Accepted Submission(s): 3067
Problem Description
现有一笔经费可以报销一定额度的发票。允许报销的发票类型包括买图书(A类)、文具(B类)、差旅(C类),要求每张发票的总额不得超过1000元,每张发票上,单项物品的价值不得超过600元。现请你编写程序,在给出的一堆发票中找出可以报销的、不超过给定额度的最大报销额。
Input
测试输入包含若干测试用例。每个测试用例的第1行包含两个正数 Q 和 N,其中 Q 是给定的报销额度,N(<=30)是发票张数。随后是 N 行输入,每行的格式为:
m Type_1:price_1 Type_2:price_2 ... Type_m:price_m
其中正整数 m 是这张发票上所开物品的件数,Type_i 和 price_i 是第 i 项物品的种类和价值。物品种类用一个大写英文字母表示。当N为0时,全部输入结束,相应的结果不要输出。
m Type_1:price_1 Type_2:price_2 ... Type_m:price_m
其中正整数 m 是这张发票上所开物品的件数,Type_i 和 price_i 是第 i 项物品的种类和价值。物品种类用一个大写英文字母表示。当N为0时,全部输入结束,相应的结果不要输出。
Output
对每个测试用例输出1行,即可以报销的最大数额,精确到小数点后2位。
Sample Input
200.00 3 2 A:23.50 B:100.00 1 C:650.00 3 A:59.99 A:120.00 X:10.00 1200.00 2 2 B:600.00 A:400.00 1 C:200.50 1200.50 3 2 B:600.00 A:400.00 1 C:200.50 1 A:100.00 100.00 0
Sample Output
123.50 1000.00 1200.50
题意: 有N张发票, 每张发票有m种物品。 现在有个最大的报销额Q, 问这些发票最多能报销多少金额.
思路: 因为是在DIY 别人开的 DP专题做的, 并且刚做了一题 01背包。 所以第一思路是 将double 值 转化为 整数。
因为是两位小数 我就乘100, 粗略算了下 900ms 勉强能过。 所以就试了下。 暴力是要 2 ^ 30次方这会超时。
1. 01 背包
int result [3000000]; 数组下标:代表报销金额。 输出 result[ (int) (Q * 100)];
#include <stdio.h>
#include <string.h>
typedef struct node {
int a, b, c, others, total;
}node;
node s[35];
int front, result[3000005];;
void add(char ch, double mon) {
int money = (int) (mon * 100);
if(ch == 'A') s[front].a += money;
else if(ch == 'B') s[front].b += money;
else if(ch == 'C') s[front].c += money;
else s[front].others += money;
s[front].total += money;
}
int f(int Q) {
if(s[front].total > 100000) return 0;
if(s[front].total > Q) return 0;
if(s[front].others > 0) return 0;
if(s[front].a > 60000 || s[front].b > 60000 || s[front].c > 60000) return 0;
return 1;
}
int main() {
double temp;
int N;
while(scanf("%lf%d", &temp, &N)) {
if(N == 0)
break;
int Q = (int) (temp * 100);
for(int i = 0; i < 35; ++i)
s[i].a = s[i].b = s[i].c = s[i].others = s[i].total = 0;
front = 0;
while(N--) {
int temp;
scanf("%d", &temp);
getchar();
while(temp--) {
char ch;
double money;
scanf("%c:%lf", &ch, &money);
getchar();
add(ch, money);
}
if(f(Q))
front++;
else
s[front].a = s[front].b = s[front].c = s[front].others = s[front].total = 0;
}
double sum = 0.0;
memset(result, 0, sizeof(result));
for(int i = 0; i < front; ++i)
for(int j = Q; j >= s[i].total; j--)
result[j] = result[j] > result[j - s[i].total] + s[i].total ? result[j] : result[j - s[i].total] + s[i].total;
printf("%.2lf\n", result[Q] / 100.0);
}
}
2.后来同学说了下, 01背包可以简化。 让我想想动态规划的定义, 把大问题,细小化。 每次处理好当前的任务就好。
下面是 动态规划
思路:从一张发票开始算到 front 张发票。 每次的任务是 处理 好当前发票与前面发票的最大报销额。
dp[] 存储的是 当前发票与前面发票的最大报销额
比如 有3张 符合条件的发票 最大报销额为 500元 3张发票的金额分别是 20 480 499;
那么现在有3个任务:
第一张发票的报销额 dp[1] = 20;
第二张发票 与 前面1张发票的最大报销额 dp[2] = 500;
第三张发票 与 前面2张发票的最大报销额 dp[3] = 499;
#include <stdio.h>
#include <string.h>
typedef struct node {
double a, b, c, others, total; //a,b,c,其他类物品, 和总额
}node;
node s[31];
int front;
void add(char ch, double money) { //计算发票
if(ch == 'A') s[front].a += money;
else if(ch == 'B') s[front].b += money;
else if(ch == 'C') s[front].c += money;
else s[front].others += money;
s[front].total += money;
}
int f(double Q) {//判断发票是否报销符合条件
if(s[front].total > 1000) return 0;
if(s[front].total > Q) return 0;
if(s[front].others > 0) return 0;
if(s[front].a > 600 || s[front].b > 600 || s[front].c > 600) return 0;
return 1;
}
int main() {
double Q;
int N;
while(scanf("%lf%d", &Q, &N)) {
if(N == 0)
break;
for(int i = 0; i < 35; ++i)
s[i].a = s[i].b = s[i].c = s[i].others = s[i].total = 0;
front = 0;
while(N--) {
int temp;
scanf("%d", &temp);
getchar();
while(temp--) {
char ch;
double money;
scanf("%c:%lf", &ch, &money);
getchar();
add(ch, money);
}
if(f(Q))
front++;
else
s[front].a = s[front].b = s[front].c = s[front].others = s[front].total = 0;
}
double dp[31], ans = 0;
memset(dp, 0, sizeof(dp));
for(int i = 0; i < front; ++i) {
for(int j = 0; j <= i; ++j) {
double temp = dp[j] + s[i].total;
if(temp <= Q && temp >= dp[i])
dp[i] = temp;
}
}
for(int i = 0; i < front; ++i)
if(ans < dp[i])
ans = dp[i];
printf("%.2lf\n", ans);
}
}