AcWing 3209. 集合竞价 Java
原题链接
简单
作者:
白嫖怪给我
,
2021-07-22 13:30:39
,
所有人可见
,
阅读 281
Java 代码
import java.util.*;
class Main{
static int N = 5010;
public static void main(String[] args){
Scanner in = new Scanner(System.in);
boolean[] st = new boolean[N];
String[] save = new String[N];
String[][] split = new String[N][5];
int i = 1;
while (in.hasNextLine()){
save[i] = in.nextLine();
split[i] = save[i].split(" ");
if (split[i][0].equals("cancel")){
//用bool数组标记取消的记录
st[Integer.parseInt(split[i][1])] = true;
}
i++;
}
//结果可能溢出int,所以要用long
long buySum = 0;
long sellSum = 0;
double p = 0.0;
long resSum = 0;
double resP = 0;
//以第i个为开盘价进行枚举
for (int j = 1; j < i; j++){
//如果当前记录被取消,则直接跳过
if (st[j] == true)continue;
if (split[j][0].equals("cancel"))continue;
p = Double.parseDouble(split[j][1]);
//第i个开盘价的情况下,求出总的bugSum和sellSum
for (int k = 1; k < i; k++){
//如果当前记录被取消,则直接跳过
if (st[k] == true)continue;
if (split[k][0].equals("cancel"))continue;
if (split[k][0].equals("buy")){
double buyPrice = Double.parseDouble(split[k][1]);
if (buyPrice >= p){
buySum += Long.parseLong(split[k][2]);
}
}else {
double sellPrice = Double.parseDouble(split[k][1]);
if (sellPrice <= p){
sellSum += Long.parseLong(split[k][2]);
}
}
}
//取出两者和的最小值
Long Sum = Math.min(buySum,sellSum);
//开盘价更大,则更新
if (Sum > resSum){
resSum = Sum;
resP = p;
}else if (Sum == resSum){
//成交量相同情况下,开盘价尽量大
resP = Math.max(resP,p);
}
buySum = 0;
sellSum = 0;
}
System.out.printf("%.2f %d",resP,resSum);
}
}