PAT 甲级 1056 JAVA

我被这题目转晕了,6, 0 , 8 原来指的是第二行的第6个, 0个,8个,我一开始理解成了第0个 6号出场, 第一个0号出场…所以做出来的总对不上

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

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 = Integer.parseInt(info1[0]);
        int M = Integer.parseInt(info1[1]);
        Mice[] mice = new Mice[N];
        String[] info2 = br.readLine().split(" ");
        String[] info3 = br.readLine().split(" ");
        for (int i = 0; i < N; i++) {
            mice[i] = new Mice();
            mice[i].id = i;
            mice[i].weight = Integer.parseInt(info2[i]);
            mice[i].playing_order = Integer.parseInt(info3[i]);
        }
        List<Mice> mice_po_l = new ArrayList<>();
        for (int i = 0; i < N; i++) {
            mice_po_l.add(mice[Integer.parseInt(info3[i])]);
        }
        StringBuilder strb = new StringBuilder();
        play(mice_po_l, M);
        for (Mice m : mice) {
            strb.append(m.rank);
            strb.append(" ");
        }
        strb.deleteCharAt(strb.length() - 1);
        System.out.println(strb);
    }

    private static class Mice {
        int id;
        int weight;
        int playing_order;
        int rank;
    }

    private static class Comparator_po implements Comparator<Mice> {

        @Override
        public int compare(Mice o1, Mice o2) {
            return o1.playing_order - o2.playing_order;
        }
    }

    private static class Comparator_w implements Comparator<Mice> {

        @Override
        public int compare(Mice o1, Mice o2) {
            return o1.weight - o2.weight;
        }
    }

    private static void play(List<Mice> cur, int M) {
        if (cur.size() == 1) {
            cur.get(0).rank = 1;
            return;
        }
        int cur_rank = cur.size() / M + 1;
        if (cur.size() % M > 0) cur_rank += 1;
        List<Mice> levelups = new ArrayList<>();
        int temp = 0;
        int temp_loc = 0;
        List<Mice> comp = new ArrayList<>();
        for (int i = 0; i < cur.size(); i++) {
            temp++;
            int c = i % M;
            comp.add(cur.get(i));
            if (temp % M == 0) {
                Collections.sort(comp, new Comparator_w());
                for (int j = 0; j < M - 1; j++) {
                    comp.get(j).rank = cur_rank;
                }
                levelups.add(comp.get(M - 1));
                temp_loc += 1;
                comp.clear();
            }
        }

        if (cur.size() % M > 0) {
            Collections.sort(comp, new Comparator_w());
            for (int j = 0; j < cur.size() % M - 1; j++) {
                comp.get(j).rank = cur_rank;
            }
            levelups.add(comp.get(comp.size() - 1));
        }
        play(levelups, M);
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值