201412-3 Collective Bidding

Problem Description
A stock exchange asks you to write a program to determine the opening price and volume of a particular stock based on the orders submitted by customers before the opening.
The input of the program consists of many lines, one record for each action, which may be as follows:
1. Buys represent a purchase order for shares, with a bid of p per hand and a purchase number of s shares.
2. Saleps means a sale order for the sale of stocks with a bid of p per hand and the number of shares sold is s.
3. cancel i denotes the cancellation of the record in line i.
If the opening price is p0, the system can match all purchase orders with at least P0 and all sales orders with at most p0. Therefore, the opening volume at this time is the smaller value between the total number of shares that bid at least for P0 and the total number of shares that bid at most for p0.
Your program needs to set an opening price so that the opening volume can be as large as possible. If there are multiple qualified opening prices, your program should output the highest one.
Input format
The input data has any number of rows, each of which is a record. Ensure that input is legitimate. The number of shares is not more than 108 positive integers, the bid is precise to the decimal point after the two positive real numbers, and not more than 10,000.00.
Output format
You need to output a line with two numbers separated by a space. The first one is the opening price, and the second one is the volume under the opening price. Opening prices need to be precise to exactly two decimal places.
sample input
buy 9.25 100
buy 8.88 175
sell 9.00 1000
buy 9.00 400
sell 8.92 400
cancel 1
buy 100.00 50
sample output
9.00 450
Assessment of use case size and conventions

For 100% of the data, the number of rows entered does not exceed 5000.

#include<iostream>
#include<iomanip>
#include<vector>
#include<algorithm>
#include<cstring> 
using namespace std;

struct log{
	char type[7];
	float price;
	long long number;
};
bool sortByPrice(const log &l1, const log &l2)
{  
    return l1.price < l2.price;//Ascending order  
}  

int main(){
	vector<log> L,B,S;
	log templog;
	char tempS[7];
	float tempP,openPrice;
	long long opentotal=0;
	cin>>templog.type;
	while(!cin.eof()){
//	while(scanf("%s",&templog.type)!=EOF){
		if(strcmp(templog.type,"cancel")==0){
			int a;
			scanf("%d",&a);
			//cin>>a;
			strcpy(L[a-1].type,"cancel");
//			cout<<L[a-1].type<<endl;
			templog.price=0;
			templog.number=0;
			L.push_back(templog);
		}
		else{
			scanf("%f",&templog.price);
			//cin>>templog.price;

			scanf("%lld",&templog.number);
			//cin>>templog.number;

			L.push_back(templog);
		}
		cin>>templog.type;
	}

	for(int i=0;i<L.size();i++){
		if(strcmp("buy",L[i].type)==0){
			B.push_back(L[i]);
		}
		else if(strcmp("sell",L[i].type)==0){
			S.push_back(L[i]);
		}
	}
	sort(L.begin(),L.end(),sortByPrice);
/*
	for(int i=0;i<L.size();i++){
		cout<<L[i].type<<" ";//<<setPrecesion(2)<<L[i].price<<
		printf("%.2f",L[i].price);
		cout<<" "<<L[i].number<<endl;
	}
*/
	for(int i=0;i<L.size();i++){
		float tempPrice=L[i].price;
		long long tempbuy=0,tempsell=0,temptotal=0;
		
		for(int j=0;j<B.size();j++)
			if(B[j].price>=tempPrice)
				tempbuy+=B[j].number; 
				
		for(int j=0;j<S.size();j++)
			if(S[j].price<=tempPrice)
				tempsell+=S[j].number; 

		temptotal=(tempbuy>tempsell?tempsell:tempbuy);
		if(temptotal>=opentotal){
			opentotal=temptotal;
			openPrice=tempPrice;
		}
	} 
	printf("%.2f",openPrice);
	cout<<" "<<opentotal;
	
}

Summary of experience:
After reading the questions, try to simulate the input.
For example, cancel deletion of a line in the question refers to the number of lines of all input, which can not be taken for granted to be deleted directly with vector.
How do scanf receive a blank line?
Note that scanf & S is char []
Precision of decimal digits: set precision?
Sort sort?

Added by ridgerunner on Fri, 05 Jul 2019 21:42:04 +0300