试题编号: | 201412-3 |
试题名称: | 集合竞价 |
时间限制: | 1.0s |
内存限制: | 256.0MB |
问题描述: | 问题描述 某股票交易所请你编写一个程序,根据开盘前客户提交的订单来确定某特定股票的开盘价和开盘成交量。 输入格式 输入数据有任意多行,每一行是一条记录。保证输入合法。股数为不超过108的正整数,出价为精确到恰好小数点后两位的正实数,且不超过10000.00。 输出格式 你需要输出一行,包含两个数,以一个空格分隔。第一个数是开盘价,第二个是此开盘价下的成交量。开盘价需要精确到小数点后恰好两位。 样例输入 buy 9.25 100 样例输出 9.00 450 评测用例规模与约定 对于100%的数据,输入的行数不超过5000。 |
java:
import java.io.BufferedInputStream;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;
class StockArray{
String SBC;
float price;
long number;
}
public class Main {
public static void main(String[] args){
Scanner scanner=new Scanner(new BufferedInputStream(System.in));
StockArray[] ss=new StockArray[5002];
for (int i = 0; i < 5002; i++) {
ss[i]=new StockArray();
}
int num=1;
while(scanner.hasNextLine()){
String a = scanner.nextLine();
if(a.trim().length()==0)
{
break;
}
String []b = a.split(" ");
ss[num].SBC=b[0];
if (ss[num].SBC.equals("buy")||ss[num].SBC.equals("sell")) {
ss[num].price=Float.parseFloat(b[1]);
ss[num].number=Long.parseLong(b[2]);
}else if (ss[num].SBC.equals("cancel")) {
ss[(int) Long.parseLong(b[1])].SBC="CANCEL";
}
num++;
}
scanner.close();
StockArray[] n1=new StockArray[num];
for (int i = 0; i < num; i++) {
n1[i]=new StockArray();
}
StockArray[] n2=new StockArray[num];
for (int i = 0; i < num; i++) {
n2[i]=new StockArray();
}
int num1=0;
int num2=0;
long ans_num=0;
float ans_price=0;
for(int i=1;i<num;i++){
if (ss[i].SBC.equals("buy")) {
n1[num1].price=ss[i].price;
n1[num1].number=ss[i].number;
num1++;
}
if (ss[i].SBC.equals("sell")) {
n2[num2].price=ss[i].price;
n2[num2].number=ss[i].number;
num2++;
}
}
Arrays.sort(n1,0,num1,new MyComprator1());
Arrays.sort(n2,0,num2,new MyComprator2());
long sum1=0, sum2;
float p;
for (int i = 0; i < num1; i++) {
p=n1[i].price;
sum1+=n1[i].number;
sum2=0;
for (int j = 0; j < num2; j++) {
if (n2[j].price>n1[i].price)
break;
sum2+=n2[j].number;
}
long min_sum=Math.min(sum1, sum2);
if (ans_num<min_sum) {
ans_num=min_sum;
ans_price=p;
}
}
System.out.printf("%.2f",ans_price);
System.out.println(" "+ans_num);
}
}
class MyComprator1 implements Comparator<Object>{
public int compare(Object o1, Object o2){
StockArray s1=(StockArray)o1;
StockArray s2=(StockArray)o2;
if (s1.price!=s2.price) {
return s2.price>s1.price ? 1: -1;
}else {
return s2.number>s1.number ? 1:-1;
}
}
}
class MyComprator2 implements Comparator<Object>{
public int compare(Object o1, Object o2){
StockArray s1=(StockArray)o1;
StockArray s2=(StockArray)o2;
if (s1.price!=s2.price) {
return s1.price>s2.price ? 1: -1;
}else {
return s1.number>s2.number ? 1:-1;
}
}
}
c++:
#include <bits/stdc++.h>
using namespace std;
struct node{
string cmd;
double price;
long long counter;
int row;
bool state;
node(){
price=0;
counter=0;
row=0;
state=true;
}
};
int main()
{
vector<node> v;
vector<node>::iterator vit,vit1;
node a;
while(cin>>a.cmd){
if(a.cmd!="cancel"){
a.row=0,cin>>a.price>>a.counter;
v.push_back(a);
}
else{
a.price=0,a.counter=0,cin>>a.row;
v.push_back(a);
v.at(a.row-1).state=false;
}
}
double price=0;
long long counter=0;//
for(vit=v.begin();vit!=v.end();vit++){
if(vit->state && vit->cmd!="cancel"){
long long tempmax=0,tempmin=0;
for( vit1=v.begin();vit1!=v.end();vit1++){
if(vit1->state && vit1->cmd=="buy" && vit1->price>=vit->price) tempmin+=vit1->counter;
if(vit1->state && vit1->cmd=="sell" && vit1->price<=vit->price) tempmax+=vit1->counter;
}
if(min(tempmin,tempmax)>counter){
counter=min(tempmin,tempmax);
price=vit->price;
}
else if(min(tempmin,tempmax)==counter){
price=max(price,vit->price);
}
}
}
cout<<fixed<<setprecision(2)<<price<<" "<<setprecision(0)<<counter<<endl;
return 0;
}