[PAT] Class 1070 Mooncake (25 points) (greedy water in water)

Meaning of the questions:

Enter two positive integers N and M (M is an integer of doubt, N <= 1000, M <= 500) represents the number of species and moon cake market demand for maximum moon cake, then the input N represents a positive integer some moon cake stock , then enter the N positive number indicates some kind of moon cake stock all the shots profits. Output maximum profit.

trick:

M test point 2 may not contain an integer data. (Although the title description surface M is a positive integer, but based on past experience PAT Class topics, there may not be an integer .....)

Code:

#define HAVE_STRUCT_TIMESPEC
#include<bits/stdc++.h>
using namespace std;
double a[1007];
double b[1007];
pair<double,int>p[1007];
int main(){
int n;
double m;
scanf("%d%lf",&n,&m);
for(int i=1;i<=n;++i)
scanf("%lf",&a[i]);
for(int i=1;i<=n;++i)
scanf("%lf",&b[i]);
for(int i=1;i<=n;++i)
p[i].first=b[i]/a[i],p[i].second=i;
sort(p+1,p+1+n);
double sum=0;
double ans=0;
for(int i=n;i;--i){
if(sum+a[p[i].second]<=m){
sum+=a[p[i].second];
ans+=b[p[i].second];
}
else{
ans+=(m-sum)*p[i].first;
break;
}
}
printf("%.2f",ans);
return 0;
}

Guess you like

Origin www.cnblogs.com/ldudxy/p/11788078.html