CCF 201412-3 集合竞价 100分

题目来源:- 计算机软件能力认证考试系统

思路分析:用结构体来描述每条记录的具体内容,如果某条记录被cancel,就将该条记录的股价和股数都记为0。将每条记录放在数组里,按股价从小到大排序。如果是buy方,就正向计算数组的前n项的和并存入buy数组中,sell方则反向计算。最后根据相同的下标判断buy和sell数组中哪个更小,求得答案。

#include<bits/stdc++.h>

using namespace std;

struct Node {
	bool isSell;//是否为卖方
	double price;//股价
	int number;//股数
} node[5010];

long long buy[5010]= {0},sell[5010]= {0};

bool cmp(Node a,Node b) {	//按股价从小到大排序,sell放左边
	if(a.price==b.price)
		return a.isSell>b.isSell;
	return a.price<b.price;
}

int main() {
	int count=1,num;
	string str;

	while(cin>>str) {
		double price,number;
		if(str=="buy") {
			cin>>node[count].price>>node[count].number;
			node[count].isSell=false;
		} else if(str=="sell") {
			cin>>node[count].price>>node[count].number;
			node[count].isSell=true;
		} else {
			cin>>num;
			node[num].price=node[num].number=0;
		}
		count++;
	}

	sort(node,node+count,cmp);

	int left=1,high=count-1;
	long long ans=0,bestans=0,cum=0;
	double p=0;

	while(node[left].price==0)         //过滤被删除的记录
        left++;

	for(int i=high; i>=left; i--) {    //正向计算buy数组
		if(node[i].isSell==false)
			cum+=node[i].number;
		buy[i]=cum;
	}
	cum=0;
	
	for(int i=left; i<=high; i++) {    //反向计算sell数组
		if(node[i].isSell==true)
			cum+=node[i].number;
		sell[i]=cum;
	}
	
	for(int i=left; i<=high; i++) {
		ans=min(sell[i],buy[i]);
		if(ans>=bestans) {
			bestans=ans;
			p=node[i].price;
		}
	}
	
	printf("%.2lf %lld",p,bestans);
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值