题解 | 分数线划定

分数线划定

https://www.nowcoder.com/practice/2395fa7b6c6e452e8d8310a7cfdbe902

import java.util.Scanner;
import java.util.*;
import java.util.stream.Collectors;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNext()) {
            int n = in.nextInt();
            int m = in.nextInt();
            LinkedHashMap<Integer, Integer> map = new LinkedHashMap<>();
            for (int i = 0; i < n; i++) {
                int k = in.nextInt();
                int s = in.nextInt();
                map.put(k, s);
            }
            int t = (int)(1.5 * m);

            List<Map.Entry<Integer, Integer>> list = map.entrySet().stream()
                    .sorted(Map.Entry.<Integer, Integer>comparingByValue(Comparator.comparing(
                                x -> x, Comparator.reverseOrder()))
                            .thenComparing(Map.Entry.comparingByKey())
                           ).collect(Collectors.toList());


            Integer line = list.get(t - 1).getValue();
            long cnt = list.stream().filter(o -> o.getValue() >= line).count();

            System.out.println(line + " " + cnt);
            list.stream().filter(o -> o.getValue() >= line).forEach(o-> {
                System.out.println(o.getKey() + " " + o.getValue());
            });

        }
    }
}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务