题目
思想
没太读懂题目的意思。除了几条对字符串的解释之外,有几个点是解题的关键。
- 此时的开盘成交量为出价至少为p0的买单的总股数和所有出价至多为p0的卖单的总股数之间的较小值。----->最后输出的应该是两者的最小值。
- 你的程序需要确定一个开盘价,使得开盘成交量尽可能地大,如果有多个符合条件的开盘价,你的程序应当输出最高的那一个。 —>输出成交量尽量大的一个,在成交量相同的情况下,成交价尽量大。
代码
#include <stdio.h>
#include <cstring>
#include <iostream>
#include <cmath>
using namespace std;
const int N = 5010;
struct Record {
//用于标记该记录时买(1)或者卖(0)
int type;
//表示价格
double p;
//表示数量
int s;
//用于标记是否被删除
//false 表示没有
//true 表示删除
bool is_del;
} d[N];
int n = 0;
int main() {
string type;
while (cin >> type) {
if (type == "buy") {
double p;
int s;
cin >> p;
cin >> s;
d[++n] = {1, p, s};
} else if (type == "sell") {
double p;
int s;
cin >> p;
cin >> s;
d[++n] = {0, p, s};
} else {
int id;
cin >> id;
d[id].is_del = true; //撤销id条操作
d[++n].is_del = true; //将该cancel设置为true
}
}
double resp = 0;
long long ress = 0;
for (int i = 1; i <= n; i++) {
//如果该记录已经被撤销就不进行操作
if (d[i].is_del == false) {
//以当前记录的价格为出手价与其他的记录中的价格比较
double p = d[i].p;
//s1表示buy的股数,s2表示sell的股数
long long s1 = 0, s2 = 0;
for (int j = 1; j <= n; j++) {
if (d[j].is_del == false) {
if (d[j].type == 1 && d[j].p >= p) {
s1 += d[j].s;
} else if (d[j].type == 0 && d[j].p <= p) {
s2 += d[j].s;
}
}
}
long long temp = fmin(s1, s2);
if (temp > ress || (temp == ress && p > resp)) {
ress = temp;
resp = p;
}
}
}
printf("%.2lf %lld\n", resp, ress);
return 0;
}
后记
菜,所以只会暴力的方法,下面给出y总代码
。