说声抱歉,这题写的代码有点乱,所以后面有时间再改,虽然AC了
先讲思路,防止以后自己忘了…
这题主要题意为多组数据,每次很多交易记录,如果此时买的时候,发现有比他内心价格还低的价格,就买,卖的时候,发现有比他心中价格高的,就卖,如果没买完或者卖完,放着剩下的等待,
如果在买或者卖之时,发现有人出价相同,那么编号在前的先来(防止打打杀杀)。
如果还有一个是废除编号第几的命令(保证只废除买或者卖的),不管那条命令是否执行完,都废除,即说白,执行完咱们就不用管了,没执行卡掉,执行一半(就是有进行买卖但有剩余)也卡掉剩下的。
然后接下来如果可以进行交易,先把交易的数量价格输出(买肯定是由小到大买),(卖肯定是由大到小卖)
每条指令quote一次 左边买右边卖,如果此时左边 莫得人 输出 0 0 ,右边莫得人 输出 0 99999
好像就没了…
思路:刚开始200多行T了…因为比较暴力每次就废除直接删,开个vector把前面的整就来,删完在整回去,而找输出的数也用vector整进来整出去…
其实不然,如果你要删,你可以先标记他不存在了就行,如果每次进行buy sell 之时顺便删了就好,
而找优先队列的第一个后面与其相同的price 数量有多少,只需要前面开个数组,存一下每个price有多少数量就行了…买和卖分开存…然后就模拟就没了
但因为本人菜,所以不能把两个很相似的东西整成函数,只会复制粘贴,,,所以后面再改吧…谢谢观看废话
#include <bits/stdc++.h>
using namespace std;
const int MA = 1e5 + 5;
const int MM = 1e6 + 6;
struct buy {
int num,prize,bian;
bool operator <(const buy b)const {
if(this-> prize == b.prize) return this->bian > b.bian;
return this-> prize < b.prize; //高在前
}
}wz;
struct sell {
int num,prize,bian;
bool operator <(const sell b)const {
if(this-> prize == b.prize) return this->bian > b.bian;
return this-> prize > b.prize; // 低在前
}
}qw;
int bma[MA],bpz[MA],bsz[MA],bpf[MA],mlpb[MM],mlps[MM];
int main()
{
int n,j; char s[10];
int kase = 0;
while(scanf("%d",&n)== 1 && n)
{
if(kase) printf("\n");
int sz,pz;
priority_queue <buy> de; priority_queue <sell> chu;
memset(bma,0,sizeof(bma));
memset(bpz,0,sizeof(bpz));
memset(bsz,0,sizeof(bsz));
memset(bpf,0,sizeof(bpf));
memset(mlpb,0,sizeof(mlpb));
memset(mlps,0,sizeof(mlps));
//初始化
for (int i = 1; i <= n; i++)
{
//加数
cin>>s;
if(s[0] == 'B' || s[0] == 'S') //买或者卖
{
scanf("%d%d",&sz,&pz);
if(s[0] == 'B') //买
{
while(!chu.empty() && !bma[chu.top().bian]) chu.pop();
while(!chu.empty()) //清空没用的
{
qw = chu.top();
if(qw.prize > pz)
{
break; // 莫得谈
}
else
{
if(qw.num > sz)
{
chu.pop();
qw.num -= sz; bsz[qw.bian] -=sz;
mlps[qw.prize] -=sz; // 三个地方要减
printf("TRADE %d %d\n",sz,qw.prize);
sz = 0;
chu.push(qw);
break;
}
else
{
chu.pop();
sz -=qw.num;
mlps[qw.prize] -=qw.num;
printf("TRADE %d %d\n",qw.num,qw.prize);
bma[qw.bian] = 0;
}
}
while(!chu.empty() && !bma[chu.top().bian]) chu.pop();
}
if(sz){
wz.num = sz; wz.prize = pz; wz.bian = i;
de.push(wz); bma[i] = 1;
bpz[i] = pz; bsz[i] = sz; bpf[i] = 1;
mlpb[pz]+=sz;
}
}
else // 卖
{
while(!de.empty() && !bma[de.top().bian]) de.pop();
while(!de.empty())
{
wz = de.top();
if(wz.prize < pz)
{
break; // 莫得谈
}
else
{
if(wz.num > sz)
{
de.pop();
wz.num -= sz; bsz[wz.bian] -=sz;
mlpb[wz.prize] -=sz;
printf("TRADE %d %d\n",sz,wz.prize);
sz = 0;
de.push(wz);
break;
}
else
{
de.pop();
sz -= wz.num; mlpb[wz.prize] -=wz.num;
printf("TRADE %d %d\n",wz.num,wz.prize);
bma[wz.bian] = 0;
}
}
while(!de.empty() && !bma[de.top().bian]) de.pop();
}
if(sz){
qw.num = sz; qw.prize = pz; qw.bian = i;
chu.push(qw); bma[i] = 1;
bpz[i] = pz; bsz[i] = sz; bpf[i] = 0;
mlps[pz]+=sz;
}
}
}
else
{
int ma; scanf("%d",&ma);
if(bma[ma]){
bma[ma] = 0;
if(bpf[ma] == 1) mlpb[bpz[ma]] -=bsz[ma];
else mlps[bpz[ma]] -=bsz[ma];
}
}
//找数
while(!de.empty() && !bma[de.top().bian]) de.pop();
while(!chu.empty() && !bma[chu.top().bian]) chu.pop();
int a1,a2,b1,b2;
if(de.empty()){
a1 = 0; a2 = 0;
}
else {
a2 = de.top().prize; a1 = mlpb[de.top().prize];
}
if(chu.empty()){
b1 = 0; b2 = 99999;
}
else {
b2 = chu.top().prize; b1 = mlps[chu.top().prize];
}
printf("QUOTE %d %d - %d %d\n",a1,a2,b1,b2);
}
kase++;
}
return 0;
}