PAT:1070. Mooncake (25) AC

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;

struct moomcake
{
  double store;      //【cation】这个可能是小数!
  double sumPrice;
  double solePrice;
}M[1010];

bool cmp(moomcake a,moomcake b)
{
  return a.solePrice>b.solePrice;
}

int main()
{
  memset(M,-1,sizeof(M));
  int n;
  double d;
  scanf("%d%lf",&n,&d);
  for(int i=0 ; i<n ; ++i)
    scanf("%lf",&M[i].store);
  for(int i=0 ; i<n ; ++i)
  {
    scanf("%lf",&M[i].sumPrice);
    M[i].solePrice=M[i].sumPrice/M[i].store;
  }
  sort(M,M+n,cmp);
  double p=0;
  for(int i=0 ; i<n && d>0; ++i)
  {
    if(M[i].store<=d)
    {
      p+=M[i].sumPrice;
      d-=M[i].store;
    }
    else if(M[i].store>d)
    {
      p+=M[i].solePrice*d;
      break;
    }
  }
  printf("%.2f\n",p);
  return 0;
}
posted on 2015-03-10 13:01  Evence  阅读(148)  评论(0编辑  收藏  举报