1070. Mooncake (25)
2015-09-09 17:30
453 查看
Mooncake (25)
时间限制
100 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue
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 200
180 150 100
7.5 7.2 4.5
Sample Output:
9.45
实质上是部分背包问题。有个需要注意的点,数量是以百万位单位的,所以不一定是整数,不能定义为int。
时间限制
100 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue
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 200
180 150 100
7.5 7.2 4.5
Sample Output:
9.45
实质上是部分背包问题。有个需要注意的点,数量是以百万位单位的,所以不一定是整数,不能定义为int。
#include<iostream> #include<algorithm> #include<vector> using namespace std; struct node { float num; float singprice; float price; node(){} void get() { cin>>price; singprice=price/num; } }; bool cmp(node a,node b) { return a.singprice>b.singprice; } int main() { //freopen("in.txt","r",stdin); int n,m; cin>>n>>m; vector<node> b(n); for(int i=0;i<n;i++) cin>>b[i].num; for(int i=0;i<n;i++) { b[i].get(); } sort(b.begin(),b.end(),cmp); float profit=0; for(int i=0;i<n&&m>0;i++) { if(m>=b[i].num) { profit+=(b[i].num*1.0)*b[i].singprice; m-=b[i].num;; } else { profit+=(m*1.0)*b[i].singprice; break; } } printf("%.2f",profit); system("pause"); return 0; }
相关文章推荐
- libdvbpsi源码分析(四)PAT表解析/重建
- PAT配置
- 什么是端口复用动态地址转换(PAT) 介绍配置实例
- MikroTik layer7-protocol
- PAT是如何工作的
- 1.一些 贪心算法 的简单思维题:
- PAT 乙级题:1002. 写出这个数 (20)
- 贪心算法——字典序最小问题
- 贪心算法——区间调度问题
- PAT (Advanced Level) Practise 1001-1010
- 数据结构学习与实验指导(一)
- Jump Game I,II 贪心
- Wildcard Matching
- PAT Basic Level 1001-1010解题报告
- 贪心题目循环和控制台折行
- 1001. 害死人不偿命的(3n+1)猜想
- 1002. 写出这个数
- 1032. 挖掘机技术哪家强
- 1001. 害死人不偿命的(3n+1)猜想 (PAT basic)
- 1002. 写出这个数(PAT Basic)