PAT 1055 Mooncake (25)(贪心,最大利润)(待修改)(官网有个测试点通不过,不知到错在哪,下次再改好了)
2016-08-25 20:54
323 查看
官网
1070. 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
解题思路
1.贪心代码
#include<iostream> #include<iomanip> #include<vector> #include<algorithm> using namespace std; struct mooncakes{ double tons,price,price_per_tons; }; bool cmp(const mooncakes& a,const mooncakes& b) { return a.price_per_tons>b.price_per_tons; } int main(){ int n;double d; cin >> n >> d; vector<mooncakes> a(n); for (int i = 0; i < n; ++i) { cin >> a[i].tons; } for (int i = 0; i < n; ++i) { cin >> a[i].price; a[i].price_per_tons = a[i].price/a[i].tons; } sort(a.begin(),a.end(),cmp); vector<double> sum(n,0); sum[0] = a[0].tons; int k=0; double profit; vector<double> p(n,0); p[0] = a[0].price; if (sum[0] > d) { profit = d*a[0].price_per_tons; } else { for (int i = 1; i < n; ++i) { sum[i] = sum[i-1] + a[i].tons; p[i] = p[i - 1] + a[i].price; if (sum[i] > d) { k = i; profit = p[k - 1] +(d - sum[k - 1]) * a[k].price_per_tons; break; } } } cout << fixed << setprecision(2) <<profit <<endl; return 0; }
相关文章推荐
- 1070. Mooncake (25)【贪心】——PAT (Advanced Level) Practise
- PAT - 甲级 - 1070. Mooncake (25)(贪心)
- ZJU-PAT 1055. The World's Richest (25) 浙大2013年上机复试第二题
- pat 1055. The World's Richest (25)
- PAT-1021. Deepest Root (25)-DFS求树的最大深度
- 【待修改】PATBasic——1010. 一元多项式求导 (25)
- PAT 1055. The World's Richest (25)
- PAT 1007. Maximum Subsequence Sum (25) 最大子序列和
- pat 1055. The World's Richest (25)
- poj 1456 贪心+并查集优化(卖带截止时间货物的最大利润)
- PAT 1033. To Fill or Not to Fill (25)(贪心)
- PAT (Advanced Level) 1007. Maximum Subsequence Sum (25) 最大连续子数组,一次遍历,stack辅助
- pat 1055. The World's Richest (25)
- PAT: 1055. The World's Richest (25)
- 【C++】PAT(advanced level)1055. The World's Richest (25)*
- PAT 1070. Mooncake (25)
- 【PAT】1055. The World's Richest (25)
- PAT (Advanced) 1055. The World's Richest (25)
- PAT 1070. Mooncake (25)
- hdu 1055 & poj 2054 Color a Tree 树&贪心 找最大费用点和父节点合并