题目解析
给定 n 种月饼 D万吨需求量 求最大收益
解题思路
贪心
让我们想想啊 如果我们是老板 我们是不是应该把单价 最贵的先给卖完 然后依次按价格从高到低卖
这样子我们的收益肯定会更高
我这里用一个对象数组去存储 n 种月饼的 库存 总价 单价
然后以单价从高到低排序
然后依次从高到低去售出 直到卖光
注: 本题精度好像有点问题 int过不掉 double可以
代码
import java.io.*;
import java.math.*;
import java.util.*;
public class Main
{
static class edge implements Comparable<edge>
{
double ku, total, price;
public edge(double ku, double total, double price)
{
this.ku = ku;
this.total = total;
this.price = price;
}
@Override
public int compareTo(edge other)
{
if (this.price > other.price)
return -1;
else if (this.price < other.price)
return 1;
return 0;
}
}
public static void main(String[] args) throws IOException
{
int n = ini();
double D = ind();
double Ku[] = new double[n + 10];
for (int i = 1; i <= n; i++)
Ku[i] = ind();
edge shu[] = new edge[n + 10];
for (int i = 1; i <= n; i++)
{
double total = ind();
shu[i] = new edge(Ku[i], total, total / Ku[i]);
}
Arrays.sort(shu, 1, n + 1);
double ans = 0;
for (int i = 1; i <= n; i++)
{
edge t = shu[i];
if (t.ku <= D)
{
ans += t.total;
D -= t.ku;
} else
{
ans += D * t.price;
break;
}
}
out.printf("%.2f\n", ans);
out.flush();
out.close();
}
static StreamTokenizer sc = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
static PrintWriter out = new PrintWriter(System.out);
static int ini() throws IOException
{
sc.nextToken();
return (int) sc.nval;
}
static double ind() throws IOException
{
sc.nextToken();
return sc.nval;
}
}