PAT (Basic Level) Practice (Chinese) 1020 moon cake (25 points)

PAT (Basic Level) Practice (中文)

announcement

PAT original site users can  https://patest.cn/bind_old_pat_user  bound to A title fight Account page. After binding, the site of the original PAT submission will be incorporated into the title fight A website user corresponding topic focus.

return

1020 moon cake (25 points)

Chinese people eat moon cake is in the Mid-Autumn Festival a traditional food, different regions have many different flavors of moon cake. Now given all kinds of moon cake inventory, total price, and the maximum demand of the market, you can get the maximum benefit calculated how much.

Note: Allow removing a portion of the stock sale. Case examples given is this: If we have three kinds of moon cake, its stocks were 18,15,10 tons, the total price respectively 75,72,45 billion. If the maximum market demand is only 20 million tons, then we should be the maximum revenue strategy to sell all of the 150,000 tons of the two kinds of moon cake, as well as 50,000 tons of the three kinds of moon cake, get 72 + 45/2 = 94.5 (million) .

Input formats:

Each input comprises a test. Each test case is given to a positive integer not more than 1000 N represents the number of types of moon cake, and no more than 500 (in units of thousands of tons) of the positive integer D represents the maximum demand of market. Then given N th row represents a positive number of stocks of each moon cake (in units of thousands of tons); N last line gives positive number indicates a total sales price of each moon cake (in units of billions). Between numbers separated by a space.

Output formats:

For each test, the maximum benefit in the output line, in units of billions and accurately to two decimal places.

Sample input:

3 20
18 15 10
75 72 45

Sample output:

94.50

 

Ideas:

   Calculating each cost moon cake, sorted by cost, greedy selected.

#include<iostream>
#include<algorithm>
using  namespace std;
struct Node{
	   double a;
	   double b;
	   double p;
}a[1009];
int cmp( struct Node a ,struct Node b ){
	return a.p > b.p;
}
int   main(void){
	  int N,M;
	  double ans;
	  while(  scanf("%d%d",&N,&M) !=EOF){
	  	 for( int i=0;i < N;i++){
	  	      scanf("%lf",&a[i].a);	 
		 }
	  	 for( int i=0;i < N;i++){
	  	      scanf("%lf",&a[i].b);	 
		      a[i].p = a[i].b/a[i].a; 
		 }
		 ans = 0;
		 sort(a,a+N,cmp);
		 for( int i=0;i<N;i++){
		       if( M >a[i].a ){
		       	   ans = ans+ a[i].b;
			       M-=a[i].a;
			   }
			   else if( M == a[i].a ){
			   	   ans = ans+ a[i].b;
			       M-=a[i].a;
			       break;
			   }
			   else if (M < a[i].a ){
			   	   ans = ans+ a[i].p*M;
			       M=0;
			       break;
			   }	
		 } 
		 printf("%0.2lf\n",ans);
	  } 
	
	  return 0;
}

 

Guess you like

Origin blog.csdn.net/S_999999/article/details/94657045