#include <stdio.h>
#include <cmath>
#include <string.h>
#include <deque>
using namespace std;
int main(int argc, char *argv[]) {
int np, ng;
scanf("%d %d", &np, &ng);
int i, j, k, in;
int *w = new int[np];
int *rank = new int[np];
deque<int> order;
memset(rank, 0, np * sizeof(int));
for (i = 0; i < np; i++) {
scanf("%d", &w[i]);
}
for (i = 0; i < np; i++) {
scanf("%d", &in);
order.push_back(in);
}
order.push_back(-1);
int winner_count = 0;
int max = -1;
int maxIndex = 0;
while (order.size() >= 3) {
winner_count = (order.size() - 1) / ng + 1;
if ((order.size() - 1) % ng == 0) winner_count--;
while (order.front() != -1) {
max = -1;
maxIndex = 0;
for (i = 0; i < ng && order[i] != -1; i++) {
if (max < w[order[i]]) {
max = w[order[i]];
maxIndex = i;
}
}
for (i = 0; i < ng && order.front() != -1; i++) {
int tmp = order.front();
order.pop_front();
if (i == maxIndex) {
order.push_back(tmp);
} else {
rank[tmp] = winner_count + 1;
}
}
}
order.pop_front();
order.push_back(-1);
}
rank[order.front()] = 1;
for (i = 0; i < np; i++) {
printf("%d", rank[i]);
if (i < np - 1) printf(" ");
}
printf("\n");
return 0;
}
【PAT甲级】1056. Mice and Rice (25)
最新推荐文章于 2021-01-26 21:09:52 发布