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();
}

 

posted @ 2019-03-11 17:14  月光下の魔术师  阅读(10)  评论(0)    收藏  举报