This article is not a problem-solving report, just published my Problem solving ideas, in this algorithm involves the storage of data, screening, sequencing, business implementation. In the end, a personal code is attached to satisfy multiple sets of test examples, not knowing if it is completely correct. Hope to see the problem of Bo friends to give advice, or for the topic there is a better way, please give a hint, thank you! PS: Because the first two posts to the first page of the article, was removed by the administrator, said the problem-solving report type of article is not suitable for publishing to the home page. This article is not a problem-solving report, what is it? I also said not good, first hair, hope to meet the communicator. RePs: It proves that I am still an article of problem-solving report type. It was removed in less than two minutes after the first page was sent! Hope not to be blacklisted! The problem is as follows: 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. Source:
#include"stdafx.h"#include<iostream>#include<string.h>#include<algorithm>using namespacestd;Const intMIN =0; typedefstructshares{//the price of buying or selling DoublePrice ; //number of shares intNumber ; //false means buy//true means selling BOOLBuyorsell; //Is cancel BOOLenable; Shares () { price=0.00; number=0; Buyorsell=false; Enable=true; }};intMinintNUM1,intnum2);//cancellation of th clause trading unitvoidCancelrecord (intth);//trading stocks that have never been classified, sorted by criteria and orderedvoidDivtypeofshares (intNint&lenofsell,int&lenofbuy);//Calculation ResultsvoidComputepriceandmax (intNDouble& Perprice,int&pernumber);//Store Input trading unit, do not distinguish between buyer and sellerShares shares[10001];//Store Buyer's trading unitShares sharesbuy[5001];//Store Seller's trading unitShares sharessell[5001];//A trading unit comparison function that defines the buyer type for sorting//The main sort increments by the price of the keyword//The order is decremented by the number of purchased shares as a keywordBOOLcmpbuy (Shares buy1, Shares buy2) {if(Buy1.price! =Buy2.price)returnBuy1.price <Buy2.price; Else if(Buy1.price = =Buy2.price)returnBuy1.number >=Buy2.number;}//Seller Type Trading unit Ibid.BOOLCmpsell (Shares sell1, Shares sell2) {if(Sell1.price! =Sell2.price)returnSell1.price >=Sell2.price; Else if(Sell1.price = =Sell2.price)returnSell1.number >=Sell2.number;}voidMainvoid){ Chartypeofshares[Ten]; DoublePrice ; intNumber ; intindex =1; DoublePerprice =0.00; intPernumber =MIN; while(Cin >>typeofshares) { if(strcmp (Typeofshares,"buy") ==0) {cin>> Price >>Number ; Shares[index].buyorsell=false; Shares[index].price=Price ; Shares[index].number=Number ; Index++; } Else if(strcmp (Typeofshares,"Sell") ==0) {cin>> Price >>Number ; Shares[index].buyorsell=true; Shares[index].price=Price ; Shares[index].number=Number ; Index++; } Else if(strcmp (Typeofshares,"Cancel") ==0) { intRecordth; CIN>>Recordth; Cancelrecord (Recordth); } Else{Computepriceandmax (index-1, Perprice, Pernumber); cout<< Perprice <<' '<< Pernumber <<Endl; } memset (Typeofshares,0,Ten); }}intMinintNUM1,intnum2) { if(Num1 <=num2)returnNUM1; Else returnnum2;}//cancellation of th clause trading unitvoidCancelrecord (intth) {Shares[th].price=0.00; Shares[th].number=0; Shares[th].enable=false;}//trading stocks that have never been classified, sorted by criteria and orderedvoidDivtypeofshares (intNint&lenofsell,int&lenofbuy) { inti =1, j =0, k =0; for(; I <= N; i++) { if(Shares[i].buyorsell = =false&& shares[i].enable = =true) Sharesbuy[j++] =Shares[i]; Else if(Shares[i].buyorsell = =true&& shares[i].enable = =true) Sharessell[k++] =Shares[i]; } lenofbuy=J; Lenofsell=K; Sort (sharesbuy, sharesbuy+lenofbuy, cmpbuy); Sort (Sharessell, Sharessell+Lenofsell, Cmpsell);}voidComputepriceandmax (intNDouble& Perprice,int&Pernumber) { DoubleTmpprice;//temporary storage of optimal open prices intSumbuy =0, Sumsell =0;//The total number of shares bought and the total number of shares sold intLenofsharesbuy, Lenofsharessell;//Buyer Type array length, seller type array lengthdivtypeofshares (n, Lenofsharessell, lenofsharesbuy); for(inti =0; i < lenofsharesbuy; i++) { /*The order of the Trades by the front buyer is known to the current trading unit whose price is greater than equal to the trading unit in front of it. Then if its shares are larger or equal than all the previous trading units, then there is no need to buy the previous trading unit. */ if(Sharesbuy[i].number >sumbuy) {Tmpprice=Sharesbuy[i].price; Sumbuy=Sharesbuy[i].number; } ElseSumbuy+=Sharesbuy[i].number; Sumsell=0; for(intj =0; J < Lenofsharessell; J + +) { //if the price of the current seller unit is higher than the open prices, it cannot be purchased, skipping if(Sharessell[j].price >Tmpprice)Continue; Sumsell+=Sharessell[j].number; } if(Pernumber <=min (sumbuy, Sumsell)) {Pernumber=min (sumbuy, Sumsell); Perprice=Tmpprice; } }}
The biggest turnover in the stock market-----A good greedy algorithm topic