Collective Bidding of CCF (java)

Keywords: Java

Question No. 201412-3
Question title: call auction
Time limit: 1.0s
Memory limitation: 256.0MB
Problem Description:
Problem description
A stock exchange asks you to write a program to determine the opening price and volume of a particular stock based on the orders submitted by customers before the opening.
The input of the program consists of many lines, one record for each action, which may be as follows:
1. Buys represent a purchase order for shares, with a bid of p per hand and a purchase number of s shares.
2. Saleps means a sale order for the sale of stocks with a bid of p per hand and the number of shares sold is s.
3. cancel i denotes the cancellation of the record in line i.
If the opening price is p0, the system can match all purchase orders with at least P0 and all sales orders with at most p0. Therefore, the opening volume at this time is the smaller value between the total number of shares that bid at least for P0 and the total number of shares that bid at most for p0.
Your program needs to set an opening price so that the opening volume can be as large as possible. If there are multiple qualified opening prices, your program should output the highest one.
Input format
The input data has any number of rows, each of which is a record. Ensure that input is legitimate. The number of shares is not more than 108 positive integers, and the bid is accurate to the two real decimal points, and does not exceed 10,000.00.
Output format
You need to output a line with two numbers separated by a space. The first one is the opening price, and the second one is the volume under the opening price. Opening prices need to be precise to two decimal places.
sample input
buy 9.25 100
buy 8.88 175
sell 9.00 1000
buy 9.00 400
sell 8.92 400
cancel 1
buy 100.00 50
sample output
9.00 450
Evaluate the size and conventions of use cases
For 100% of the data, the number of rows entered does not exceed 5000.

Solution code (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;
        }   
    }
   


}

Posted by atyndall on Fri, 29 Mar 2019 00:33:28 -0700