PAT B - - 1020 moon cake

1. pair used often do not remember a period of time, this good https://blog.csdn.net/qq_41431457/article/details/88583448

2. The meaning of the questions is to bring the highest price, in order to sell the unit price decreasing, we first calculate the unit price reordering, with pari can get the unit price in descending order index, recorded.

This idea should be more concise, less than forty lines can be resolved

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

Author: CHEN, Yue

Unit: Zhejiang University

Time limit: 150 ms

Memory Limit: 64 MB

Code length limit: 16 KB


#include <iostream>
#include <algorithm>
using namespace std;

int main(){
	
	int N;
	cin>>N; 
	double n[N];//库存量 
	double d[N];//总售价 
	pair<double,int> a[N];//月饼单价 
	int D;
	cin>>D;
	for(int i=0;i<N;i++){
		cin>>n[i];//月饼库存量 
	}
	for(int i=0;i<N;i++){
		cin>>d[i];//月饼总售价 
		a[i]=pair<double,int>((d[i]/n[i]),i);
	}
	sort(a,a+N);

	double profit = 0;
	for(int i=N-1;i>=0;i--){
		if(n[a[i].second]>D){
			profit+=a[i].first*D;
			break;
		}
		D-=n[a[i].second];
		if(D<0){
			profit+=a[i].first*(D+n[a[i].second]);
			break;
		}else if(D>=0){
			profit+=a[i].first*n[a[i].second];
		}
	}
	printf("%.2lf",profit);
	
	return 0;
}

 

Published 228 original articles · won praise 76 · views 60000 +

Guess you like

Origin blog.csdn.net/qq_41895747/article/details/102969087