Mooncake (排序+贪心)

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、这种题目,最好把所以进行运算的变量都设为double。2、最大总量可能为0。

  1 #include <iostream>
  2
  3 #include <algorithm>
  4
  5 #include <iomanip>
  6
  7 using namespace std;
  8
  9
 10
 11 struct moom
 12
 13 {
 14
 15    double pr;
 16
 17    double cc;
 18
 19 };
 20
 21
 22
 23 bool cmp(moom a,moom b)
 24
 25 {
 26
 27    return a.pr/a.cc>b.pr/b.cc;
 28
 29 }
 30
 31
 32
 33 moom mm[1000];
 34
 35
 36
 37 int main()
 38
 39 {
 40
 41       int n,i;
 42
 43       double d;
 44
 45       while(cin>>n)
 46
 47       {
 48
 49          cin>>d;
 50
 51
 52
 53          for(i=0;i<n;i++)
 54
 55          {
 56
 57
 58
 59                cin>>mm[i].cc;
 60
 61          }
 62
 63
 64
 65           for(i=0;i<n;i++)
 66
 67          {
 68
 69
 70
 71                cin>>mm[i].pr;
 72
 73          }
 74
 75
 76
 77             sort(mm,mm+n,cmp);
 78
 79
 80
 81             i=0;double sum=0;
 82
 83             while(true)
 84
 85             {
 86
 87                if(mm[i].cc==0) break;
 88
 89                if(d<=mm[i].cc)
 90
 91                {
 92
 93                   sum=sum+d/mm[i].cc*mm[i].pr;
 94
 95                     break;
 96
 97                }
 98
 99                else
100
101                {
102
103                   d=d-mm[i].cc;
104
105                     sum=sum+mm[i].pr;
106
107                     i++;
108
109                }
110
111             }
112
113
114
115       cout<<fixed<<setprecision(2)<<sum<<endl;
116
117       }
118
119    return 0;
120
121 }
122
123
124
125  

时间: 2024-10-25 14:59:32

Mooncake (排序+贪心)的相关文章

2014 Super Training #8 B Consecutive Blocks --排序+贪心

当时不知道怎么下手,后来一看原来就是排个序然后乱搞就行了. 解法不想写了,可见:http://blog.csdn.net/u013368721/article/details/28071241 其实就是滑动窗口的思想. 代码: #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <cstdlib> #include <algor

HDU 4912 Paths on the tree LCA 排序贪心

lca... 排个序然后暴力保平安 _(:зゝ∠)_ #pragma comment(linker, "/STACK:102400000,102400000") #include"cstdio" #include"iostream" #include"set" #include"queue" #include"string.h" using namespace std; #define

CodeForces 1294B Collecting Packages(排序+贪心)

http://codeforces.com/contest/1294/problem/B 大致题意: 一张图上有n个包裹,给出他们的坐标,一个机器人从(0,0)出发,只能向右(R)或向上(U),问能否收集到所有包裹,如果能,给出字典序最小的路径. 最开始当成搜索题了,其实可以排序+贪心写的. 1 #include <stdio.h> 2 #include <string.h> 3 #include <iostream> 4 #include <string>

ACM学习历程—HihoCoder1309任务分配(排序 &amp;&amp; 贪心)

http://hihocoder.com/problemset/problem/1309 题目大意是给定n个任务的起始时间,求问最少需要多少台机器. 有一个贪心的策略就是,如果说对于一个任务结束,必然接一个开始时间最接近这个的比较合算.我们假想一个任务池,那么任务池中最早结束的那个,必然接剩余任务中最早开始的比赛合算(相同开始时间最早结束),而且假设这个任务是P,那么对于一个结束时间晚于P的任务,显然后面的一段时间是浪费的,同样对于一个开始时间晚于P的任务,前面一段是浪费的: 关键在于某个开始时

poj1456 结构体排序+贪心

题意:给出很多商品,每个商品有价值和出售期限,只能在期限内出售才能获取利润,每一个单位时间只能出售一种商品,问最多能获得多少利润. 只需要按照优先价值大的,其次时间长的排序所有物品,然后贪心选择,从它可以选的时间开始往前遍历,如果某个时间点没有出售过商品,那就放在那个时间出售,就这样就行. 1 #include<stdio.h> 2 #include<string.h> 3 #include<algorithm> 4 using namespace std; 5 6 s

bzoj 1604: [Usaco2008 Open]Cow Neighborhoods 奶牛的邻居——排序+贪心+set

Description 了解奶牛们的人都知道,奶牛喜欢成群结队.观察约翰的N(1≤N≤100000)只奶牛,你会发现她们已经结成了几个"群".每只奶牛在吃草的时候有一个独一无二的位置坐标Xi,Yi(l≤Xi,Yi≤[1..10^9]:Xi,Yi∈整数.当满足下列两个条件之一,两只奶牛i和j是属于同一个群的: 1.两只奶牛的曼哈顿距离不超过C(1≤C≤10^9),即lXi - xil+IYi - Yil≤C. 2.两只奶牛有共同的邻居.即,存在一只奶牛k,使i与k,j与k均同属一个群.

洛谷P2127 序列排序 [贪心]

题目传送门 题目描述 小C有一个N个数的整数序列,这个序列的中的数两两不同.小C每次可以交换序列中的任意两个数,代价为这两个数之和.小C希望将整个序列升序排序,问小C需要的最小代价是多少? 输入输出格式 输入格式: 第一行,一个整数N. 第二行,N个整数,表示小C的序列. 输出格式: 一行,一个整数,表示小C需要的最小代价. 输入输出样例 输入样例#1: 复制 6 8 4 5 3 2 7 输出样例#1: 复制 34 说明 数据范围: 对于30%的数据,1<=N<=10: 对于全部的数据,1&l

vijos 1605 双栈排序 - 贪心 - 二分图

题目传送门 传送门I 传送门II 题目大意 双栈排序,问最小字典序操作序列. 不能发现两个数$a_{j}, a_{k}\ \ (j < k)$不能放在同一个栈的充分必要条件时存在一个$i$使得$j < k < i$且$a_{i} < a_{j} < a_{k}$. 证明?写个dfs就证完了(就是考虑任意一个三元组). 然后可建图,对于满足上面条件的$(j, k)$,$j$和$k$连一条无向边. 显然必要条件时图不存在奇环,即能够二分染色. 再证明一下这是必要条件. 我们先构造

Educational Codeforces Round 85 B. Middle Class(排序/贪心/水题)

Many years ago Berland was a small country where only nn people lived. Each person had some savings: the ii -th one had aiai burles. The government considered a person as wealthy if he had at least xx burles. To increase the number of wealthy people