1056 Mice and Rice (25point(s)) - C语言 PAT 甲级

1056 Mice and Rice (25point(s))

Mice and Rice is the name of a programming contest in which each programmer must write a piece of code to control the movements of a mouse in a given map. The goal of each mouse is to eat as much rice as possible in order to become a FatMouse.

First the playing order is randomly decided for NP programmers. Then every NG programmers are grouped in a match. The fattest mouse in a group wins and enters the next turn. All the losers in this turn are ranked the same. Every NG winners are then grouped in the next match until a final winner is determined.

For the sake of simplicity, assume that the weight of each mouse is fixed once the programmer submits his/her code. Given the weights of all the mice and the initial playing order, you are supposed to output the ranks for the programmers.

Input Specification:

Each input file contains one test case. For each case, the first line contains 2 positive integers: NP and NG (≤ 1000), the number of programmers and the maximum number of mice in a group, respectively. If there are less than NG mice at the end of the player’s list, then all the mice left will be put into the last group. The second line contains NP distinct non-negative numbers Wi (i=0,⋯,N​P​​−1) where each W​i​​ is the weight of the i-th mouse respectively. The third line gives the initial playing order which is a permutation of 0,⋯,NP−1 (assume that the programmers are numbered from 0 to NP−1). All the numbers in a line are separated by a space.

Output Specification:

For each test case, print the final ranks in a line. The i-th number is the rank of the i-th programmer, and all the numbers must be separated by a space, with no extra space at the end of the line.

Sample Input:

11 3
25 18 0 46 37 3 19 22 57 56 10
6 0 8 7 10 5 9 1 4 2 3

Sample Output:

5 5 5 2 5 5 5 3 1 3 5

题目大意:
  • 输入 np 只老鼠的体重,其序号从 0 递增,
  • 第二行输入老鼠序号,即老鼠的出场顺序,每 ng 只老鼠为一组,每组中体重最大的老鼠晋级
  • 按照老鼠的序号输出每只老鼠的排名
设计思路:

队列模拟晋级

  • 队列存储老鼠出场的序号
  • 每 ng 只老鼠中选择一只晋级,直到队列中剩余一只老鼠结束
    • 每一轮未晋级的老鼠排名等于,本轮次中晋级老鼠的数量加 1
    • 即等于本轮次中分组的数量加 1
编译器:C (gcc)
#include 

int main(void)
{
        int np, ng;
        int weights[1001] = {0}, ranks[1001] = {0}, queue[1001] = {0};
        int rank, max, count, index;
        int i;

        scanf("%d %d", &np, &ng);
        for (i = 0; i < np; i++) {
                scanf("%d", &weights[i]);
        }
        for (i = 0; i < np; i++) {
                scanf("%d", &queue[i]);
        }

        count = np;
        while (count > 1) {
                rank = (count % ng) ? (count / ng + 2): (count / ng + 1);
                index = 0;
                max = queue[0];
                for (i = 1; i < count; i++) {
                        if (i % ng == 0) {
                                queue[index] = max;
                                index++;
                                max = queue[i];
                        } else {
                                if (weights[queue[i]] > weights[max]) {
                                        ranks[max] = rank;
                                        max = queue[i];
                                } else {
                                        ranks[queue[i]] = rank;
                                }
                        }
                }
                queue[index] = max;
                index++;
                count = index;
        }
        ranks[queue[0]] = 1;

        printf("%d", ranks[0]);
        for (i = 1; i < np; i++) {
                printf(" %d", ranks[i]);
        }

        return 0;
}

你可能感兴趣的:(PAT,甲级,PAT,甲级,C语言)