ccf--Set Auction (simulation)

Source: Internet
Author: User
Tags valid

Topic:

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 P 0, then the system can match all bids with at least P 0 and all bids up to P 0 for a sell order. As a result, the opening volume at this time is a small value between the total number of shares that are bid at least P 0 and all bids up to P 0 for the total number of shares sold.
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 10 8, and the bid is accurate to the positive real number of two digits after the decimal point and not exceeding 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. Ideas:

The key is the selection of P0, here P0 must be a valid buy or sell orders p.
Reason:
If P0 is not a buy or sell order p, then p0 must be somewhere between the two bid p, set to P1 and P2, that is P1 < P0 < P2;
1. When the buy and sell orders have P2, then if the P0 for the unit price, it will inevitably reduce the sell orders, because the volume is taken small, so the volume will be reduced, as the opening price for the P2 is good;
2. When the purchase price is P2, and the opening is P2, the opening price will not be the p0;
3. When only sell orders have P2, the situation and 1 are the same;
4. When the buy and sell orders have P1 or only when the purchase of P1, if the p0 for the unit price, it will reduce the bill, and then the volume decreases, as the open Price is P1;
5. When only sell orders have P1, then and open price for P1 is the same, at this time p0 better than P1, but P2 is necessarily better than p0; because the purchase order is not P1, so at this time for the P1,P0,P2 to pay the same volume, and sell a single volume P1 and p0 the same, p2 is greater than equals P1 and P 0;

Code:

#include <iostream> #include <cstdio> #include <algorithm> #include <set> using namespace std;
typedef long Long LL;
	#define MAX 5007 typedef struct{string m;
	Double p;
	int s;
BOOL E;
}r;
R R[max];
Set<double> St;
	int main () {/* Each record here needs to be recorded, including Cancel I, because cancel I is a record in the case containing the cancel I record */int m=0;
			while (CIN&GT;&GT;R[M].M) {if (r[m].m = = "Cancel") {cin>>r[m].s;
			R[r[m].s-1].e=true;
			m++;
		Continue
		} cin>>r[m].p>>r[m].s;
	++m;
	
	} for (int i=0;i<m;++i) if (r[i].m! = "Cancel" &&!r[i].e) St.insert (R[I].P);
	ll M_sum=0;
	Double p=0;
		For (Set<double>::iterator It=st.begin (); It!=st.end (); ++it) {double p0 = *it;
		ll Sumb=0,sums=0,sum;
		for (int i=0;i<m;++i) if (r[i].m = = "sell" &&!r[i].e && r[i].p<=p0) Sums+=r[i].s;
		for (int i=0;i<m;++i) if (r[i].m = = "buy" &&!r[i].e && r[i].p>=p0) Sumb+=r[i].s;
		
		Sum=min (SUMS,SUMB); if (sum>=m_sum){m_sum=sum;
		P=p0;
	}}//note reserved to two digits after the decimal point printf ("%.2f", p);	
	cout<<m_sum<<endl;
return 0; }


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.