您的位置:首页 > 其它

1020. 月饼 (25)

2015-08-11 13:51 239 查看



时间限制

100 ms

内存限制

65536 kB

代码长度限制

8000 B

判题程序

Standard

作者

CHEN, Yue

月饼是中国人在中秋佳节时吃的一种传统食品,不同地区有许多不同风味的月饼。现给定所有种类月饼的库存量、总售价、以及市场的最大需求量,请你计算可以获得的最大收益是多少。

注意:销售时允许取出一部分库存。样例给出的情形是这样的:假如我们有3种月饼,其库存量分别为18、15、10万吨,总售价分别为75、72、45亿元。如果市场的最大需求量只有20万吨,那么我们最大收益策略应该是卖出全部15万吨第2种月饼、以及5万吨第3种月饼,获得 72 + 45/2 = 94.5(亿元)。

输入格式:

每个输入包含1个测试用例。每个测试用例先给出一个不超过1000的正整数N表示月饼的种类数、以及不超过500(以万吨为单位)的正整数D表示市场最大需求量。随后一行给出N个正数表示每种月饼的库存量(以万吨为单位);最后一行给出N个正数表示每种月饼的总售价(以亿元为单位)。数字间以空格分隔。

输出格式:

对每组测试用例,在一行中输出最大收益,以亿元为单位并精确到小数点后2位。
输入样例:
3 20
18 15 10
75 72 45

输出样例:
94.50

没用sort排序函数,有最大情况下超时

#include <stdio.h>
#define MAX 1009

struct cake
{
double Inventory;
double Price;
}Cake[MAX];

void Input(int n,cake c[]);
void Average(int n,cake c[],double avg[]);
void Sort(int n,cake c[],double avg[],int d);

int main()
{
int N,D;
while(scanf("%d %d",&N,&D)!=EOF)
{
Input(N,Cake);
double Avg[MAX];
Average(N,Cake,Avg);
Sort(N,Cake,Avg,D);
}
return 0;
}

void Input(int n,cake c[])
{
int i;
for(i=0;i<n;i++)
{
scanf("%lf",&c[i].Inventory);
}
for(i=0;i<n;i++)
{
scanf("%lf",&c[i].Price);
}
}

void Average(int n,cake c[],double avg[])
{
int i;
for(i=0;i<n;i++)
{
avg[i]=c[i].Price*1.0/c[i].Inventory;
}
}

void Sort(int n,cake c[],double avg[],int d)
{
int i,j;
double t;
double max=0;
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(avg[i]<avg[j])
{
t=c[i].Inventory;
c[i].Inventory=c[j].Inventory;
c[j].Inventory=t;

t=c[i].Price;
c[i].Price=c[j].Price;
c[j].Price=t;

t=avg[i];
avg[i]=avg[j];
avg[j]=t;
}
}
if(c[i].Inventory<d)
{
max=max+c[i].Price;
d=d-c[i].Inventory;
}
else if(c[i].Inventory==d)
{
max=max+c[i].Price;
break;
}
else
{
max=max+avg[i]*d;
break;
}
}
printf("%.2lf\n",max);
}


#include<stdio.h>
#include<algorithm>
using namespace std;
struct N{
double num;
double total;
double price;
}data[1100];

bool cmp(N a,N b){
return a.price>b.price;
}
int main(){
int n,m;
scanf("%d%d",&n,&m);

for(int i=0;i<n;i++)
scanf("%lf",&data[i].num);

for(int i=0;i<n;i++)
scanf("%lf",&data[i].total);
for(int i=0;i<n;i++)
data[i].price=data[i].total/data[i].num;
sort(data,data+n,cmp);
double x=0;
int j=0;
while(m>0&&j<n){
if(m>data[j].num){
m-=data[j].num;
x+=data[j].total;
}
else{

x+=data[j].price*m;
m-=data[j].num;
}
j++;

}

printf("%.2lf\n",x);

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: