java实现 洛谷 P1056 排座椅_java

java实现 洛谷 P1056 排座椅_计算_02

import java.util.Arrays;
import java.util.Map.Entry;
import java.util.Scanner;
import java.util.TreeMap;
 
public class Main {
	private static Scanner cin;
	private static int m;
	private static int n;
	private static int k;
	private static int l;
	private static int d;
	public static void main(String args[]) throws Exception {
		cin = new Scanner(System.in);
		String t = cin.nextLine();
		String[] v = t.split(" ");
		m = Integer.valueOf(v[0]);
		n = Integer.valueOf(v[1]);
		k = Integer.valueOf(v[2]);
		l = Integer.valueOf(v[3]);
		d = Integer.valueOf(v[4]);
		
		int x,y,p,q;
		TreeMap<Spliter,Integer> tmK = new TreeMap<>();
		TreeMap<Spliter,Integer> tmL = new TreeMap<>();
		for(int i=0;i<d;i++) {
			t = cin.nextLine();
			v = t.split(" ");
			x = Integer.valueOf(v[0]);
			y = Integer.valueOf(v[1]);
			p = Integer.valueOf(v[2]);
			q = Integer.valueOf(v[3]);
			//the same column, x is the same, spliter is L
			if(x == p) {
				Spliter s = new Spliter((y+q)/2);
				if(tmL.containsKey(s)) {
					int count = tmL.get(s);
					tmL.replace(s, new Integer(count+1));
				}else {
					tmL.put(s, 1);
				}
			}
			//the same row, y is the same ,spliter is K
			else if(y == q) {
				Spliter s = new Spliter((x+p)/2);
				if(tmK.containsKey(s)) {
					int count = tmK.get(s);
					tmK.put(s, new Integer(count+1));
				}else {
					tmK.put(s, 1);
				}
			}
		}
		
		SpliterCount[] scK = new SpliterCount[tmK.size()];
		SpliterCount[] scL = new SpliterCount[tmL.size()];
		Entry<Spliter,Integer> e;
		int[] karray = new int[k];
		int[] larray = new int[l];
		for(int i=0,size=tmK.size();i<size;i++) {
			e = tmK.pollFirstEntry();
			SpliterCount sct = new SpliterCount(e.getKey(),e.getValue());
			scK[i] = sct;
		}
		Arrays.sort(scK);
		
		for(int i=0;i<k;i++) {
			karray[i] = scK[scK.length-i-1].getSpliter().getSpliterLine();
		}
		Arrays.sort(karray);
		
		for(int i=0,size=tmL.size();i<size;i++) {
			e = tmL.pollFirstEntry();
			SpliterCount sct = new SpliterCount(e.getKey(),e.getValue());
			scL[i] = sct;
		}
		Arrays.sort(scL);
		for(int i=0;i<l;i++) {
			larray[i] = scL[scL.length-i-1].getSpliter().getSpliterLine();
		}
		Arrays.sort(larray);
		
		String ts = Arrays.toString(karray);
		System.out.println(ts.substring(1, ts.length()-1).replace(",", ""));
		ts = Arrays.toString(larray);
		System.out.println(ts.substring(1, ts.length()-1).replace(",", ""));
	}
}
 
class Spliter implements Comparable{
	Integer spliterLine;
	public Spliter(Integer aspliterLine) {
		spliterLine = aspliterLine;
	}
	
	public Integer getSpliterLine() {
		return spliterLine;
	}
	
	public int compareTo(Object arg0) {
		Spliter s = (Spliter)arg0;
		return spliterLine.compareTo(s.getSpliterLine());
	}
	
	public String toString() {
		return String.format("spliterLine:%d,", spliterLine);
	}
}
 
class SpliterCount implements Comparable{
	Spliter spliter;
	Integer count;
	public SpliterCount(Spliter s, Integer c) {
		spliter = s;
		count = c;
	}
	
	public int compareTo(Object arg0) {
		SpliterCount sc = (SpliterCount)arg0;
		Spliter s2 = sc.getSpliter();
		Integer c2 = sc.getCount();
		if(spliter == s2 && count == c2) {
			return 0;
		}else if(count == c2) {
			return - spliter.compareTo(s2);
		}else {
			return count.compareTo(c2);
		}
	}
	
	public Spliter getSpliter() {
		return spliter;
	}
	public Integer getCount() {
		return count;
	}
	public String toString() {
		return String.format("spliter:%s count=%d", spliter.toString(),count);
	}
}