problem Description A stock exchange ask you to write a program that determines the opening and opening volume of a particular stock based on an order submitted by a pre-opening customer.
The input of the program consists of a number of lines, each of which has a record that may have the following types of records:
1. Buy P s represents the purchase price of a stock, each bid is p and the number of shares purchased is S.
2. Sell P S represents a sell order for the sale of shares, with a bid of p for each hand and a sale of shares of S.
3. Cancel I means that the record of line I is revoked.
If the open price is P0, the system can match all bids with at least P0 and all bids up to p0. As a result, the opening volume at this time is a small value between the total number of bids at least P0 and the total number of shares of all bids up to p0.
Your program needs to determine an open price so that the opening volume is as large as possible. If there are multiple open prices that match the conditions, your program should output the highest one. Input format input data has any number of lines, each row is a record. Ensure that the input is valid. The number of shares is a positive integer not exceeding 108, and the bid is accurate to the positive real number of two digits after the decimal point, and not more than 10000.00. Output format you need to output a line, containing two numbers, separated by a space. The first number is the open price and the second is the volume under this open price. The opening price needs to be exactly two digits after the decimal point. 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 evaluation case size and convention for 100% of data, the number of rows entered does not exceed 5000.
Problem Solving Ideas:Open prices can only be a buy and sell in a certain price, and because if the number of shares can be sold the same as the price of the larger one as the open price, so only in the buy order to find the opening price, so as to ensure that the topic requirements, but also reduce the time expenditure. After writing the code submission only 80 points, and then the bid for all commands in descending order, again submitted or 80 points, the visible sort is useless for this problem. Never know where the problem is. The code for the 80-minute version is given below:
1#include <stdio.h>2#include <string.h>3 4 structcharge{5 Chartra[Ten];6 floatPrice ;7 Long Longnum;8 };9 structCharge order[5005];Ten One intMainintargcConst Char*argv[]) { A intI, j, k =1 ; - intLine ; - Long Longmaxnum=0 ; the floatP0 =0.0, flag =0.0; - while(SCANF ("%s", Order[k].tra)! =EOF) -{//Enter a record - if(strcmp ("buy", order[k].tra) = =0|| strcmp"Sell", order[k].tra) = =0){ +scanf"%f%lld", &order[k].price, &order[k].num); - } + if(strcmp ("Cancel", order[k].tra) = =0){ Ascanf"%d", &line);//Row to undo atOrder[k].price =0.0;//no price and quantity under Cancel Chitian, all set to 0 -Order[k].num =0 ; -Order[line].price =0.0;//set the price and quantity of the command to be revoked to 0, which is equal to canceling the Operation -Order[line].num =0 ; - } -K + +; in } - for(i =1; I < K; i++) to{//sort by bid descending + intindex; - floatMax; the structcharge temp; *Max =Order[i].price; $ for(j = i+1; J < K; J + +)Panax Notoginseng { - if(Order[j].price >=max) { theMax =Order[j].price; +index =J; A } the } +temp =Order[i]; -Order[i] =Order[index]; $Order[index] =temp; $ } - for(i =1; I <k; i + +) -{//check each line of command, set the price of buy command as open prices the Long Longsellnum=0, buynum=0, num; - if(strcmp (Order[i].tra,"buy")==0)WuyiP0 =Order[i].price; the Else Continue ; - for(j =1; J < K; J + +) Wu{//If P0 is the open price, check all trading commands. - if(strcmp (Order[j].tra,"buy")==0&& Order[j].price >=p0) { AboutBuynum + = Order[j].num;//for buy, if the bid is greater than or equal to P0, purchase the required number of shares $ } - if(strcmp (Order[j].tra,"Sell")==0&& Order[j].price <=p0) { -Sellnum + = Order[j].num;//for the Sell command, if the bid is less than or equal to P0, the number of shares to be sold - } A } +num = (buynum >= sellnum)? Sellnum:buynum;//The number of shares that can be traded should be the smaller number between purchase and sale the if(Num > Maxnum) {maxnum = num; flag = P0;}//looking for the biggest number of shares that can be traded. - } $printf"%.2f%lld\n", flag, maxnum); the return 0; the}
Tomorrow CCF exam, ask ~ ~ ~
CCF2014123 Collection Auction (C language Edition)