【CCF】集合竞价

试题名称: 集合竞价
时间限制: 1.0s
内存限制: 256.0MB
问题描述:
问题描述
  某股票交易所请你编写一个程序,根据开盘前客户提交的订单来确定某特定股票的开盘价和开盘成交量。
  该程序的输入由很多行构成,每一行为一条记录,记录可能有以下几种:
  1. buy p s 表示一个购买股票的买单,每手出价为p,购买股数为s。
  2. sell p s 表示一个出售股票的卖单,每手出价为p,出售股数为s。
  3. cancel i表示撤销第i行的记录。
  如果开盘价为p0,则系统可以将所有出价至少为p0的买单和所有出价至多为p0的卖单进行匹配。因此,此时的开盘成交量为出价至少为p0的买单的总股数和所有出价至多为p0的卖单的总股数之间的较小值。
  你的程序需要确定一个开盘价,使得开盘成交量尽可能地大。如果有多个符合条件的开盘价,你的程序应当输出最高的那一个。
输入格式
  输入数据有任意多行,每一行是一条记录。保证输入合法。股数为不超过108的正整数,出价为精确到恰好小数点后两位的正实数,且不超过10000.00。
输出格式
  你需要输出一行,包含两个数,以一个空格分隔。第一个数是开盘价,第二个是此开盘价下的成交量。开盘价需要精确到小数点后恰好两位。
样例输入
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
样例输出
9.00 450
评测用例规模与约定

  对于100%的数据,输入的行数不超过5000。


自己编了一个程序,结果一直在等待评测。

上网找别人的代码提交也一样是卡在等待评测上了。

但是提交其它的题目没有问题。

按Ctrl+Z退出输入。

该代码可能存在问题,慎用。

第二天看了一下,通过了,运行了那么久...


long long不能用在VC6.0

#include<iostream>
#include<string>
#include<iomanip>
using namespace std;

class Node{
public:
	double price;
	long long amount;
	int index;
	Node* next;
	Node(){
		index = 0;
		price = 0;
		amount = 0;
		next = NULL;
	}
};

class LinkedList{
private:
	Node* head;
	Node* tail;
	int size;
public:
	LinkedList(){
		head = NULL;
		tail = NULL;
		size = 0;
	}
	void insert(double price,long long amount,int index){
		if(head == NULL){
			head = new Node();
			head->price = price;
			head->amount = amount;
			head->index = index;
			tail = head;
			size++;
		}
		else{
			Node *s = new Node(),*p = head;
			s->price = price;
			s->amount = amount;
			s->index = index;
			if( price > p->price){
				s->next = p;
				head = s;
				size++;
				return;
			}
			while(p->next && p->next->price > price){
				p = p -> next;
			}
			while(p->next && price == p->next->price && p->next->amount > amount){
				p = p -> next;
			}
			if(p->next){
				s -> next = p->next;
				p -> next = s;
				size++;
			}
			else{
				p -> next = s;
				size++;
			}
		}
	}

	void cancel(int index){
		Node *p,*s=head;
		while(s->index!=index && s->next){
			p = s;
			s = s->next;
		}
		if(s==head){
			head = head -> next;
			size--;
			delete s;
			return;
		}
		if(s->next){
			p->next = s->next;
			size--;
			delete s;
		}
		else{
			p->next = NULL;
			size--;
			delete s;
		}
	}

	void deleteFirst(){
		if(head){
			Node* p = head;
			head = head->next;
			size--;
			delete p;
		}
	}

	Node* getFirst(){
		return head;
	}

	void display(){
		Node*p=head;
		while(p){
			cout<<p->price<<" "<<p->amount<<endl;
			p = p -> next;
		}
	}
};

long long a = 0;
double p = 0.00;
int cancel[5000] = {0};//0表示无,1表示买,2表示卖,3表示取消
int length = 0;

void Z(LinkedList& buy,LinkedList& sell){
	int i = 1;
	while(buy.getFirst() && sell.getFirst()){
		if(buy.getFirst()->price >= sell.getFirst()->price){
			if(buy.getFirst()->amount < sell.getFirst()->amount){
				sell.getFirst()->amount -= buy.getFirst()->amount;
				a += buy.getFirst()->amount;
				p = buy.getFirst()->price;
				buy.deleteFirst();
			}
			else if(buy.getFirst()->amount > sell.getFirst()->amount){
				buy.getFirst()->amount -= sell.getFirst()->amount;
				a += sell.getFirst()->amount;
				p = buy.getFirst()->price;
				sell.deleteFirst();
			}
			else{
				a += sell.getFirst()->amount;
				p = buy.getFirst()->price;
				sell.deleteFirst();
				buy.deleteFirst();
			}
		}//bprice>=s.price
		else{
			sell.deleteFirst();
		}
	}
	cout<<setprecision(2)<<fixed<<p<<" "<<a<<endl;
}

int main(){
	LinkedList buy;
	LinkedList sell;
	string s;
	int c = -1;
	long long a;
	double p;
	while(cin>>s){
		if(s=="buy"){
			c = 0;
		}
		else if(s=="sell"){
			c = 1;
		}
		else if(s=="cancel"){
			c = 2;
		}
		else if(s=="EOF"){
			break;
		}
		switch(c){
		case 0:
			cin>>p>>a;
			buy.insert(p,a,length);
			cancel[length++] = 1;
			break;
		case 1:
			cin>>p>>a;
			sell.insert(p,a,length);
			cancel[length++] = 2;
			break;
		case 2:
			cin>>c;
			cancel[length++] = 3;
			switch(cancel[c-1]){
			case 1:
				buy.cancel(c-1);
				break;
			case 2:
				sell.cancel(c-1);
				break;
			};
			break;
		};
	}
//	buy.display();
//	cout<<endl;
//	sell.display();
	Z(buy,sell);
//	system("pause");
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值