Remember the A La Mode of the Blue Bridge Cup - Needs careful handling of logical relationships (c++ implementation)

Links above: Energy Necklace for Blue Bridge Cup-Analog Hafman Tree (c++)

Resource constraints

Time limit: 1.0s memory limit: 256.0MB

Problem Description

Hugh Samston runs a catering service that provides desserts to participants in this year's ICPC World Final.He will serve the pies with ice cream on them.To meet different needs, he prepared many different pies and ice cream.
Hugh wants to serve dessert as an ice cream on a pie.However, as a businessman, he wants to make as much money as possible.He knows the price of different types of pies and ice cream combinations, and he knows that ice cream and those pies can't be combined.
Hugh wants to determine the range of profits he can make based on the number of each pie and ice cream and the different combinations mentioned earlier.

Input Format

The format that test data input must satisfy.
The first line of input contains two integers, P and I, representing the number of types of pies and ice cream.
The next line contains P integers representing the number of slices of each type.
The next line contains I integers representing the number of ice creams of each type.
Next P lines, each containing I real numbers, represent the result of each type of combination of pie and ice cream.
If the pie and the ice cream can be combined, it is a real number (0,10) indicating the benefit of the combination.
Otherwise, -1 means the two cannot be combined.

Output Format

Output a line to output the range of profit in the form of'(Minimum Income) to'(Maximum Income).

Please note: All the pies and ice cream must be used up.

sample input

2 3
40 50
27 30 33
1.11 1.27 0.70
-1 2 0.34

sample output

91.70 to 105.87

Data size and conventions

0 < P, I <= 50, no more than 100 for each type of pie or ice cream.

My thoughts on the algorithm

  • The values of a combination array other than -1 should be sorted according to the question.
    1. When you get data from large to small, you get the most benefit from processing it
    2. Minimum benefit from data processing when data is available from small to large
  • The algorithm itself is not difficult, but it is important to note that the storage variables used are sufficient for calculation.The algorithm I wrote does not optimize the use of space, but the general idea can be referred to.

Algorithm display

#include <iostream>
#include <iomanip>
using namespace std;
double find_min(int Ps[],int Is[],int sign[],double PsIp[],int P,int I,int i,int tempn,double MIN)
{
	int min;
	double minv; 
	//Record Minimum Income
	while(tempn>0)
	{
		min=0,minv=10.0;
		for(i=0;i<(P*I);i++)
		{
			if(sign[i]>0)//sign[i]==0 means logged, sign[i]==-1 means cannot be combined 
			{
				if(minv>PsIp[i])
				{
					minv = PsIp[i];
					min  = i;
				}
			} 
			
		}
		//Calculate Minimum Income 
		if(Ps[min/I]<Is[min%I])
		{
			MIN+=Ps[min/I]*minv;
			Is[min%I]=Is[min%I]-Ps[min/I];
			tempn-=Ps[min/I];
			Ps[min/I]=0;
		}else
		{
			MIN+=Is[min%I]*minv;
			Ps[min/I]=Ps[min/I]-Is[min%I];
			tempn-=Is[min%I];
			Is[min%I]=0;
		}
		//Processing data 
		sign[min]=0;
	}
	return MIN;
} 
double find_max(int Ps[],int Is[],int sign[],double PsIp[],int P,int I,int i,int tempx,double MAX)
{
	int max;
	double maxv; 
	//Record maximum return
	while(tempx>0)
	{
		max=0,maxv=0.0;
		for(i=0;i<(P*I);i++)
		{
			if(sign[i]>0)//sign[i]==0 means logged, sign[i]==-1 means cannot be combined 
			{
				if(maxv<PsIp[i])
				{
					maxv = PsIp[i];
					max  = i;
				}
			} 
		}
		
		//Calculate maximum return 
		if(Ps[max/I]<Is[max%I])
		{
			MAX+=Ps[max/I]*maxv;
			Is[max%I]=Is[max%I]-Ps[max/I];
			tempx-=Ps[max/I];
			Ps[max/I]=0;
		}else
		{
			MAX+=Is[max%I]*maxv;
			Ps[max/I]=Ps[max/I]-Is[max%I];
			tempx-=Is[max%I];
			Is[max%I]=0;
		}
		
		//Processing data 
		sign[max]=0;
	}
	return MAX;
} 
int main()
{
	int P,I;
	int Ps[51],Is[51],Psx[51],Isx[51],sign[2501];
	double PsIp[2501],MIN=0.0,MAX=0.0;
	cin>>P>>I;
	int i;
	
	//output data 
	int tempn=0,tempx;
	for(i=0;i<P;i++)
	{
		cin>>Ps[i];
		tempn+=Ps[i];
		Psx[i] = Ps[i];
	}
	for(i=0;i<I;i++)
	{
		cin>>Is[i];
		Isx[i]=Is[i];
	}
	for(i=0;i<(P*I);i++)
	{
		cin>>PsIp[i];
		if(PsIp[i]==-1)sign[i]=-1;
		else sign[i]=1;
	}
	
	tempx = tempn;
	MIN = find_min(Ps,Is,sign,PsIp,P,I,i,tempn,MIN);
	
	//Restore the unmarked state of Psip[i] 
	for(i=0;i<(P*I);i++)
	{
		if(PsIp[i]==-1)sign[i]=-1;
		else sign[i]=1;
	}
	
	//Record maximum return
	MAX=find_max(Psx,Isx,sign,PsIp,P,I,i,tempx,MAX);
	
	cout.setf(ios::fixed);
	cout<<fixed<<setprecision(2)<<MIN<<" to "<<MAX; 
	return 0;
}
18 original articles published, 2 praised, 1636 visited
Private letter follow

Tags: iOS

Posted on Tue, 11 Feb 2020 00:36:52 -0500 by _tina_