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