⭐201412-3 集合竞价

下面这个代码会显示运行错误,90分

#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include <cstring>
#include <map> 
#include <vector>
#include<string>
#include<algorithm>
#include<queue>
using namespace std;
const int maxn = 5010;
struct Buy {
    
    
	int flag;
	double p;
	int s;
}bs[maxn];
priority_queue<double> q;//存储开盘价
int sum;
int realsum;
int sumb;
int sums;
char op[10];//操作
bool cmp(Buy a, Buy b)
{
    
    
	if (a.flag != b.flag) return a.flag < b.flag;
	else if (a.p != b.p) return a.p < b.p;
	else return 1;
	
}
int main()
{
    
    
	sum = 0;
	realsum = 0;
	sumb = sums = 0;
	int temp;
	for (int i = 0; i < maxn; i++)
	{
    
    		
		bs[i].flag = 3;
	}
	while (scanf("%s",op) != EOF)
	{
    
    			
		if (op[0] == 'b'||op[0]=='s')
		{
    
    		
			sum++;
			realsum++;
			if (op[0] == 'b')
			{
    
    
				//printf("遇到了buy\n");
				bs[sum].flag = 1;
				sumb++;
			}
			else
			{
    
    
				//printf("遇到了sell\n");
				bs[sum].flag = 2;
				sums++;
			}			
			scanf("%lf %d",&bs[sum].p,&bs[sum].s);
			//printf("sum=%d,flag=%d,p=%.2f,s=%d\n", sum,bs[sum].flag, bs[sum].p, bs[sum].s);
			//printf("sumb=%d,sums=%d\n",sumb,sums);
			q.push(bs[sum].p);				
		}		
		else
		{
    
    			
			scanf("%d", &temp);
			if (bs[temp].flag == 1)
				sumb--;
			else
				sums--;
			bs[temp].flag =3;			
			realsum--;	
			sum++;
		}
	}
	sort(bs+1, bs+1 + sum, cmp);
	//1-sumb是买进,sumb+1到sumb+sums是卖出
	/*for (int i = 1; i <= realsum; i++)
	{
		printf("flag=%d,p=%.2f,s=%d\n", bs[i].flag, bs[i].p, bs[i].s);
	}*/
	//printf("sumb=%d\n",sumb);
	long long max=0;
	long long tempmax;
	double tempp=0;
	double ans;
	long long maxb;
	long long maxs;
	while (!q.empty())
	{
    
    
		maxb = maxs = 0;
		if (tempp != q.top())
		{
    
    
			tempp = q.top();
			q.pop();
			int i;
			for (i = 1; i <= realsum; i++)
			{
    
    
				if (bs[i].p >= tempp && i <= sumb)
				{
    
    
					
					maxb += bs[i].s;
				}
					
				if (bs[i].p <= tempp && i > sumb)
				{
    
    
					maxs += bs[i].s;
				}
					
			}
			tempmax = min(maxb, maxs);
			if (tempmax > max)
			{
    
    
				max = tempmax;
				ans = tempp;				
			}			
		}
		else
		{
    
    
			q.pop();
		}
			
	}
	printf("%.2f %lld\n", ans, max);
	
	
}

参考满分代码

#include<iostream>
#include<cstdio>
#include<string>
#include<vector>
#include<queue>
#include<stack>
#include<algorithm>
using namespace std;
 
#define MAX 5000+5
#define INF 0x3f3f3f3f
#define ll long long
#define LDT priority_queue<GG, vector<GG>, LT>
 
struct GG{
    
     // 交易
	int num;
	ll sum;
	double price;
};
 
int fg[MAX];double p = 0; // fg标记是否取消
ll sum1 = 0, sum2 = 0; // 买入量与卖出量
 
struct LT{
    
     // 优先队列自定义排序函数(小顶)
	bool operator ()(const GG &g1,const GG &g2){
    
    
		return g1.price > g2.price;
	}
};
 
LDT qin; LDT qout; // 买入与卖出堆
 
void can(LDT &q,bool add){
    
     // 取消-区分是否求加和
	LDT tmp;
	while (!q.empty()){
    
    
		GG tg = q.top(); q.pop();
		if (fg[tg.num] != INF) tmp.push(tg);
	}
	while (!tmp.empty()){
    
    
		GG tg = tmp.top(); tmp.pop();
		q.push(tg); sum1 += add ? tg.sum : 0;
	}
}
 
void dooo(){
    
     //总-取消
	LDT tmp;
	can(qin, true);
	can(qout, false);
}
 
int main(){
    
    
	string s; int n = 0;
	while (cin >> s && ++n){
    
     // 读入命令并计数
		if (s == "cancel"){
    
     // 取消操作
			int kn;
			cin >> kn;
			fg[kn] = INF; // 取消,可通过修改这里修复BUG
		}
		else{
    
     // 交易操作
			double pri; ll su;
			cin >> pri >> su; // 读入信息
			GG ng; ng.num = n; // 创建交易实例
			ng.price = pri; ng.sum = su;
			if (s == "buy"){
    
     // 加入买入交易序列
				qin.push(ng);
			}
			else if (s == "sell"){
    
     // 加入卖出交易序列
				qout.push(ng);
			}
			else{
    
     // else
				break; 
			}
		}
	}
	dooo(); // do cancel
	double px; ll sum =0;
	while (!qin.empty()){
    
     // 遍历买入价
		GG ng = qin.top(); qin.pop();
		if (qout.empty()) break;
		while (!qout.empty() && qout.top().price <= ng.price){
    
    
			sum2 += qout.top().sum;
			qout.pop();
		} // 所有低于买入价的全部卖出
		if (sum <= min(sum1, sum2)){
    
     // 更新成交量(较小值)
			p = ng.price;
			sum = min(sum1, sum2);
		}
		sum1 -= ng.sum; // 提高买入价
	}
	printf("%.2lf %lld\n", p,sum); // 按格式打印结果
	return 0;
}

猜你喜欢

转载自blog.csdn.net/susuate/article/details/120115081
今日推荐