1070. Mooncake (25)

来源:互联网 发布:windows php zip扩展 编辑:程序博客网 时间:2024/06/09 08:43
题目链接:http://www.patest.cn/contests/pat-a-practise/1070
题目:

Mooncake is a Chinese bakery product traditionally eaten during the Mid-Autumn Festival. Many types of fillings and crusts can be found in traditional mooncakes according to the region's culture. Now given the inventory amounts and the prices of all kinds of the mooncakes, together with the maximum total demand of the market, you are supposed to tell the maximum profit that can be made.

Note: partial inventory storage can be taken. The sample shows the following situation: given three kinds of mooncakes with inventory amounts being 180, 150, and 100 thousand tons, and the prices being 7.5, 7.2, and 4.5 billion yuans. If the market demand can be at most 200 thousand tons, the best we can do is to sell 150 thousand tons of the second kind of mooncake, and 50 thousand tons of the third kind. Hence the total profit is 7.2 + 4.5/2 = 9.45 (billion yuans).

Input Specification:

Each input file contains one test case. For each case, the first line contains 2 positive integers N (<=1000), the number of different kinds of mooncakes, and D (<=500 thousand tons), the maximum total demand of the market. Then the second line gives the positive inventory amounts (in thousand tons), and the third line gives the positive prices (in billion yuans) of N kinds of mooncakes. All the numbers in a line are separated by a space.

Output Specification:

For each test case, print the maximum profit (in billion yuans) in one line, accurate up to 2 decimal places.

Sample Input:
3 200180 150 1007.5 7.2 4.5
Sample Output:
9.45

分析:
排序,贪心算法
AC代码:
#include<stdio.h>#include<algorithm>using namespace std;struct mooncake{ float amount; float money; float price; mooncake():amount(0),money(0),price(0){}};bool cmp(mooncake A, mooncake B){ return A.price > B.price;}int main(){ freopen("F://Temp/input.txt", "r", stdin); int N; int  D; scanf("%d%d", &N, &D); mooncake *cake = new mooncake[N]; for (int i = 0; i < N; i++){  scanf("%f", &cake[i].amount); } for (int i = 0; i < N; i++){  scanf("%f", &cake[i].money);  cake[i].price = cake[i].money  * 1.0 / cake[i].amount; } sort(cake, cake + N, cmp); float sum = 0; int idx = 0; while (D > 0){  if (D > cake[idx].amount){   D -= cake[idx].amount;   sum += cake[idx].money;   idx++;  }  else{   sum += D * cake[idx].money * 1.0 / cake[idx].amount;   D = 0;   idx++;  } } printf("%.2f\n", sum); return 0;}
提交,段错误,原因是因为idx可能访问到>=的元素,因为可能存在D很大用不完的情况。
#include<stdio.h>#include<algorithm>using namespace std;struct mooncake{ float amount; float money; float price; mooncake():amount(0),money(0),price(0){}};bool cmp(mooncake A, mooncake B){ return A.price > B.price;}int main(){ freopen("F://Temp/input.txt", "r", stdin); int N; int  D; scanf("%d%d", &N, &D); mooncake *cake = new mooncake[N]; for (int i = 0; i < N; i++){  scanf("%f", &cake[i].amount); } for (int i = 0; i < N; i++){  scanf("%f", &cake[i].money);  cake[i].price = cake[i].money  * 1.0 / cake[i].amount; } sort(cake, cake + N, cmp); float sum = 0; int idx = 0; while (D > 0){  if (D > cake[idx].amount){   D -= cake[idx].amount;   sum += cake[idx].money;   idx++;  }  else{   sum += D * cake[idx].money * 1.0 / cake[idx].amount;   D = 0;   idx++;  }  if (idx >= N)break; //增加判定退出之后,没有段错误,全AC } printf("%.2f\n", sum); return 0;}



截图:


——Apie陈小旭
0 0
原创粉丝点击