洛谷P1056 排座椅 java实现 用Set和Map解决排序 包含java中Map按value值进行排序的方法

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

    public class Main {
        //这个函数是按map的value值由大到小排序的函数
        public static void f(Map map,int k) {
            List<Map.Entry<Integer,Integer>>list=new ArrayList<Map.Entry<Integer,Integer>>(map.entrySet());
            
            Collections.sort(list,new Comparator<Map.Entry<Integer,Integer>>() {
            	//由大到小排序
                public int compare(Entry<Integer, Integer> o1, Entry<Integer, Integer> o2) {
                    return o2.getValue()-(o1.getValue());
                }
            });
            
            int num=0;
            //由要求知要按顺序输出key值,TreeSet的底层是红黑树,因此要把按map的value值排序的前k位的key按从小到大的顺序输出
            TreeSet<Integer>set=new TreeSet<Integer>();
            for(Map.Entry<Integer,Integer>mapping:list) {
            	if(num<k)set.add(mapping.getKey());
            	num++;
            }
            num=0;
            //按要求输出即可
            for(Integer s:set) {
            	if(num==k-1)System.out.println(s);
            	else { System.out.print(s+" ");num++;}
            }
        }
        public static void main(String[] args) {
            Scanner in=new Scanner(System.in);
            //按题目要求输入
            int m,n,k,l,d;
            m=in.nextInt();
            n=in.nextInt();
            k=in.nextInt();
            l=in.nextInt();
            d=in.nextInt();
            //创建两个map分别存
            //maphang的key用来存在哪一行分隔
            //value用来记录在这一行分隔的比重
            
            //maplie同理
            Map<Integer,Integer>maplie=new TreeMap<>();
            Map<Integer,Integer>maphang=new TreeMap<>();
            int x,y,p,q;
            for(int i=0;i<d;i++) {
                x=in.nextInt();
                y=in.nextInt();
                p=in.nextInt();
                q=in.nextInt();
                //核心步骤,边读入边处理
                //当x==p说明行相等,需要在列之间加上一个分隔
                //否则说明列相等,就需要在行之间加上分割
                if(x==p) {
                	y=Math.min(y, q);
                	if(maplie.containsKey(y)) {
                		maplie.put(y, maplie.get(y)+1);
                	}
                	else {
                		maplie.put(y, 1);
                	}
                }
                else {
                	x=Math.min(x, p);
                	if(maphang.containsKey(x)) {
                		maphang.put(x, maphang.get(x)+1);
                	}
                	else {
                		maphang.put(x, 1);
                	}
                }
             }
           f(maphang,k);
           f(maplie,l);		
      }
    }

 

 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值