201412-3------------集合竞价

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 5010;
struct node{
	string type;
	double p;
	int num;
};
node rec[N], sell[N], buy[N];
bool cmp_buy(node a, node b){
	return a.p > b.p;
}
bool cmp_sell(node a, node b){
	return a.p < b.p;
}
int main(){
	string str;
	double p;
		int num, n = 0, bn = 0, sn = 0;
	while(cin >> str){
		if (str != "cancel"){
			cin >> rec[n].p >> rec[n].num;
			rec[n].type = str;
			n ++;
		}
		else{
			int del;
			cin >> del;
			rec[n].type = str;
			rec[n].num = del;
			n ++;
		}
	}
//	cout << 1  << endl;
		for (int i = n - 1; i >= 0; i --){
		if (rec[i].type == "cancel"){
			if (rec[rec[i].num - 1].type != "cancel"){
				rec[rec[i].num - 1].type = "";
			}
		}else if (rec[i].type == "buy"){
			buy[bn] = rec[i], bn ++;
		}else if (rec[i].type == "sell"){
			sell[sn] = rec[i], sn ++;
		}
	} 
		sort(rec, rec + n, cmp_buy);
	sort(buy, buy + bn, cmp_buy);
	sort(sell, sell + sn, cmp_sell);
	//	for (int i = 0; i < sn; i ++){
//	cout << sell[i].type << sell[i].p << sell[i].num << endl;
//	}
		//cout << 2 << endl;
		double p0, suc_p;
	long long buy_num = 0, sell_num = 0, temp_num = 0, suc_num = 0;
	for (int i = 0; i < n; i ++){
		if (rec[i].p == 0)   break;
		buy_num = 0;
		sell_num = 0;
		p0 = rec[i].p;
		for (int j = 0; j < bn; j ++){
			if (buy[j].p < p0)   break;
			else   buy_num += buy[j].num;
		}
		for (int j = 0; j < sn; j ++){
			if (sell[j].p > p0)   break;
			else sell_num += sell[j].num;
		}
	//	cout << i << " " << p0 << endl;
		temp_num = min(buy_num, sell_num);
      //  cout << i <<" " <<temp_num << endl;
		if (temp_num > suc_num){
			suc_num = temp_num;
			suc_p = p0;
		}
	} 
		printf("%.2f %lld\n", suc_p, suc_num);
		return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值