• java实现 洛谷 P1056 排座椅


    在这里插入图片描述

    在这里插入图片描述

    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);
    	}
    }
    
  • 相关阅读:
    鲁迅说过搜索引擎
    下载github上文件与release的安装包-解决s3.amazonaws.com问题
    作业九----DFA最小化
    作业八----非确定的自动机NFA确定化为DFA
    作业七----正规式到正规文法与自动机
    作业六----正规文法与正规式
    第五次作业----词法分析程序的设计与实现
    第四次作业
    作业三
    2.文法和语言
  • 原文地址:https://www.cnblogs.com/a1439775520/p/13076801.html
Copyright © 2020-2023  润新知