CCF:201412-3集合竞价

题解:

  • 思路还是很简单的,就是暴力模拟。
  • 注意如果cancel的行还是cancel,那么上一个cancel的记录不用管,不用想太复杂。出题人没解释清楚。
  • 详细处理见代码。

代码:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int>pii;
const string s1 = "buy",s2 = "sell",s3 = "cancel";
const int N = 5000 + 10;
double const eps = 1e-8;
string s,tmp;
int cnt1,cnt2,cnt,mark,n;
vector<double>price;
pii mp[N];   
struct Node
{
	double price;
	ll num;
	bool flag;   //判断是否有效
}buy[N],sell[N];
void Init(){
	while(cin>>s){   
		++n;
		if(s == s1){
			++cnt1;
			cin>>buy[cnt1].price>>buy[cnt1].num;   //如果直接buy[++cnt1]则会出问题
			price.push_back(buy[cnt1].price);
			buy[cnt1].flag = true;
			mp[n] = pii(0,cnt1);    //0表示buy,cnt1表示在buy中的第几条记录
		}
		else if(s == s2){
			++cnt2;
			cin>>sell[cnt2].price>>sell[cnt2].num;
			price.push_back(sell[cnt2].price);
			sell[cnt2].flag = true;
			mp[n] = pii(1,cnt2);    //1表示sell,cnt2表示在sell中的第几条记录
		}else if(s == s3){    //撤销第i行的记录,撤销的可能是cancel,所以之前的撤销要恢复。
			mp[n] = pii(2,0);
			cin>>mark;
			pii p = mp[mark];
			if(p.first == 0)	buy[p.second].flag = false;
			else if(p.first == 1) sell[p.second].flag = false;
		}
	}
}
void solve(){
	sort(price.begin(),price.end());
	price.erase(unique(price.begin(),price.end()),price.end());
	ll ans = 0;
	double p;
	for(int k=0;k<price.size();k++){
		ll num1 = 0,	num2 = 0;
		for(int i=1;i<=cnt1;i++)	if(buy[i].flag && buy[i].price >= price[k]	){
			num1 += buy[i].num;
		}
		for(int i=1;i<=cnt2;i++)	if(sell[i].flag && sell[i].price <= price[k]){
			num2 += sell[i].num;
		}
		if(min(num1,num2) >= ans)	ans = min(num1,num2),	p = price[k];
	}
	printf("%.2f %lld",p,ans);
}
int main(){
	Init();
	solve();
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值