集合竞价csp

1.运行结果

2.解题思路

首先根据题意可知最优的价格一定是sell或buy中的价格,先按价格从小到大进行排序,筛掉cancel的那些行,再分别利用两个数组sell和buy分别记录价格小于等于pricei的数量和价格大于等于pricei的数量,直接遍历找出最多的数量,注意数量相同的情况下价格尽可能高,所以倒序遍历。

But犯了一个低级错误(最后输出的价格是double类型但顺手打成int 。。。。QAQ)导致一直debug。。。。

3.满分代码

#include<bits/stdc++.h>
using namespace std;
struct node{
	bool type;//类型 
	double p;//价格 
	int s;//数量 
}a[5005];
long long sell[5005],buy[5005];
bool cmp(node x,node y)//按价格从小到大排序 
{
   if(x.p!=y.p)
   return x.p<y.p;
   else
   return x.type<y.type;	
} 
int main()
{
	string s;
	int cnt=1;
	while(cin>>s)
	{
		
		if(s=="sell")
		{
			a[cnt].type=false;
			cin>>a[cnt].p>>a[cnt].s;
		}
		else if(s=="buy")
		{
			a[cnt].type=true;
			cin>>a[cnt].p>>a[cnt].s;
		}
		else
		{
			int cancel;
			cin>>cancel;
			a[cancel].p=0.0;
			a[cancel].s=0;
		}
		cnt++;
	}

	sort(a+1,a+cnt,cmp);
	int left=1,right=cnt-1;
	while(a[left].p==0)left++;//去除要删除的记录 
	long long tmp=0;
	for(int i=left;i<=right;i++)// sell[i]记录价格小于等于a[i].price的数量 
	{
		if(!a[i].type)tmp+=a[i].s;
		sell[i]=tmp;
	}
	tmp=0;
	for(int i=right;i>=left;i--)//buy[i]记录价格大于等于a[i].price的数量 
	{
		if(a[i].type)tmp+=a[i].s;
		buy[i]=tmp;
	}
	long long total=0;
	double bestprice=0.0;//一定注意数据类型!!! 
	for(int i=right;i>=left;i--)
	{
		long long sum=min(sell[i],buy[i]);
		if(total<sum)
		{
			total=sum;
			bestprice=a[i].p;
		}
	}
	printf("%.2lf %lld",bestprice,total);
	return 0;
}

  • 8
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值