PAT 甲级 1055 JAVA

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Comparator;
import java.util.PriorityQueue;

public class Main {
    public static void main (String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String[] info1 = br.readLine().split(" ");
        int N_person = Integer.parseInt(info1[0]);
        int N_query = Integer.parseInt(info1[1]);
        Person[] people = new Person[N_person];
        for (int i = 0; i < N_person; i++) {
            String[] info2 = br.readLine().split(" ");
            String name = info2[0];
            int age = Integer.parseInt(info2[1]);
            int worth = Integer.parseInt(info2[2]);
            people[i] = new Person(name, age, worth);
        }
        String[][] queries = new String[N_query][];
        for (int i = 0; i < N_query; i++) {
            queries[i] = br.readLine().split(" ");
        }
        for (int i = 0; i < N_query; i++) {
            String[] q = queries[i];
            int capacity = Integer.parseInt(q[0]);
            int min_age = Integer.parseInt(q[1]);
            int max_age = Integer.parseInt(q[2]);
            PriorityQueue<Person> pq = new PriorityQueue<>(new Comparator2());
            for (Person p : people) {
                if (p.age <= max_age && p.age >= min_age) {
                    pq.add(p);
                }
            }
            System.out.println("Case #" + (i + 1) + ":");
            if (pq.size() == 0) {
                System.out.println("None");
            } else {

                for (int j = 0; j < capacity; j++) {
                    Person cur = pq.remove();
                    System.out.println(cur.name + " " + cur.age + " " + cur.worth);
                    if (pq.size() == 0) break;
                }
            }
        }
    }

    private static class Person {
        String name;
        int age;
        int worth;
        Person (String name, int age, int worth) {
            this.name = name;
            this.age = age;
            this.worth = worth;
        }
    }

    private static class Comparator2 implements Comparator<Person> {

        @Override
        public int compare(Person o1, Person o2) {
            if (o1.worth != o2.worth) {
                return o2.worth - o1.worth;
            } else {
                if (o1.age != o2.age) {
                    return o1.age - o2.age;
                } else {
                    return o1.name.compareTo(o2.name);
                }
            }
        }
    }
}

超时2个

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值