CCF-竞价集合-Java

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;

public class Acution {
	static Scanner sc;
	static List<Record> list1;
	static Map<String,Integer> map;
	static String temp;
	static String[] cmd;
	static double p0;
	static int buys,sells,min;
	public static void main(String[] args) {
		new Acution().run();
	}
	
	public static void run() {
		sc = new Scanner(System.in);
		list1 = new ArrayList<>();
		map = new LinkedHashMap<>();
		
		while(sc.hasNextLine()) {	
			temp = sc.nextLine()+" ";
			cmd = temp.split(" ");
			if(cmd.length==3) {
				
				list1.add(new Record(cmd[0],cmd[1],Integer.parseInt(cmd[2])));
			} else if(cmd.length==2){
			
				list1.remove(Integer.parseInt(cmd[1])-1);
			} 
		}
		
		deal();

		List<Map.Entry<String, Integer>> list = new ArrayList<Map.Entry<String, Integer>>(map.entrySet());
		
		Collections.sort(list, new Comparator<Map.Entry<String, Integer>>() {

			@Override
			public int compare(Entry<String, Integer> o1, Entry<String, Integer> o2) {
				//按照值降序排列
				if(o1.getValue()!=o2.getValue()) {
					return o2.getValue().compareTo(o1.getValue());
				} else {
					//如果值相等的时候  那么按照键降序排列
					double index1 = Double.parseDouble(o2.getKey());
					double index2 = Double.parseDouble(o1.getKey());
					return (int) (index1-index2);
				} 
			}
		});
		System.out.print(list.get(0).getKey()+" "+list.get(0).getValue());
		
		
	
	}
	
	public static void deal() {
		for(int i =0;i<list1.size();i++) {
			p0 = Double.parseDouble(list1.get(i).price);
			buys=0;
			sells=0;
			min=0;
			for(int j=0;j<list1.size();j++) {
				if(list1.get(j).kind.equals("buy") && Double.parseDouble(list1.get(j).price)>=p0) {
					buys += list1.get(j).total;
				} else if(list1.get(j).kind.equals("sell") && Double.parseDouble(list1.get(j).price)<=p0) {
					sells += list1.get(j).total;
				} else {
					continue;
				}
			}
			
			min = Math.min(buys, sells);
			map.put(list1.get(i).price, min);
		}
	}
}

class Record{
	public String kind;
	public String price;
	public int total;
	public Record(String kind, String price, int total) {
		this.kind = kind;
		this.price = price;
		this.total = total;
	}
}

猜你喜欢

转载自blog.csdn.net/kidchildcsdn/article/details/83963354