AcWing 1542. 老鼠和大米-java-队列
原题链接
简单
作者:
Astarion
,
2021-03-10 18:12:51
,
所有人可见
,
阅读 390
要点:1.使用队列处理每组比赛与每轮比赛;2.使用分数与得到该分数的人数计算排名
import java.io.*;
import java.util.Map;
class Main {
static InputStreamReader isr = new InputStreamReader(System.in);
static BufferedReader in = new BufferedReader(isr);
static OutputStreamWriter osw = new OutputStreamWriter(System.out);
static BufferedWriter out = new BufferedWriter(osw);
static final int N = 1010;
static int np, ng;
//从顺序到编号的映射
static int[] order = new int[N];
//重量
static int[] w = new int[N];
//最大分数
static int maxGrade = 0;
//得分,每晋级一轮加一分,由分数得到排名
static int[] grade = new int[N];
//该分数对应的人数,用于求排名
static int[] gCnt = new int[N];
//队列实现比赛过程
static int[] queue = new int[N*4];
static int head, tail;
//根据分数求排名
static int getRank(int i) {
int rank = 1;
int g = grade[i];
for (int j = maxGrade; j > g; j--) rank += gCnt[j];
return rank;
}
public static void main(String[] args) throws IOException {
String[] strs = in.readLine().split(" ");
np = Integer.parseInt(strs[0]);
ng = Integer.parseInt(strs[1]);
strs = in.readLine().split(" ");
for (int i = 0; i < np; i++) w[i] = Integer.parseInt(strs[i]);
strs = in.readLine().split(" ");
for (int i = 0; i < np; i++) order[i] = Integer.parseInt(strs[i]);
//按顺序全部入队
for (int i = 0; i < np; i++) queue[tail++] = order[i];
int cnt = 0, end = np;
int maxW = -1, maxId = -1;
while (tail > head) {
int x = queue[head++];
if (w[x] > maxW) {
maxW = w[x];
maxId = x;
}
//记录当前组优胜者,进入下一轮
if (++cnt == ng || head == end) {
grade[maxId] ++;
queue[tail++] = maxId;
maxGrade = Math.max(maxGrade, grade[maxId]);
gCnt[maxGrade]++;
gCnt[maxGrade - 1]--;
maxId = -1;
maxW = -1;
cnt = 0;
if (head == end) {
end = tail;
if (end - head == 1) break;
}
}
}
//根据分数求排名
for (int i = 0; i < np; i++) {
int rank = getRank(i);
out.write(rank + " ");
}
in.close();
isr.close();
out.flush();
out.close();
osw.close();
}
}