1056. Mice and Rice (25)

来源:互联网 发布:北京市道路网数据下载 编辑:程序博客网 时间:2024/06/06 04:09

1056. Mice and Rice (25)

时间限制
100 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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,...NP-1) where each Wi 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 325 18 0 46 37 3 19 22 57 56 106 0 8 7 10 5 9 1 4 2 3
Sample Output:
5 5 5 2 5 5 5 3 1 3 5

解析:这道题对于竞技规则不了解的我真的好费劲, 查阅了网友的解释,题意总结如下:

有n个老鼠,第一行给出n个老鼠的重量,第二行给出他们的顺序。
1.每一轮分成若干组,每组m个老鼠,不能整除的多余的作为最后一组。
2.每组重量最大的进入下一轮。
让你给出每只老鼠最后的排名。
很简单,用两个数组模拟一下即可
order1存储进入当前一轮老鼠的索引顺序
order2存储进入下一轮老鼠的索引顺序

如果当前有groups个组,那么会有groups个老鼠进入下一轮,则没有进入下一轮的排名都为groups+1
如果只有一个组,那么最大的那个排名即为1。

(这段摘自http://www.cnblogs.com/chenxiwenruo/p/6519890.html)

这道题还是很有代表性的,但是还是可以把这道题从难到易的进行分解:

1,从头到尾遍历数组A,找到A的最大值---->太简单了

2,数组A从头到尾3个一组,找到每组内的最大值,并存到数组B中---->还行,不算难,就是多了一层对组的遍历,然后在每组内对"组员"遍历

3,数组A从头到尾3个一组,找到每组内的最大值,并还存到数组A中, ----->哈哈,这个也不难

4,重复3,直到数组A中只剩一个元素--->再来一层循环, 恩,稍加控制条件,不难

代码如下:

/*************************************************************************> File Name: 1056.c> Author: YueBo> Mail: yuebowhu@163.com> Created Time: Tue 23 May 2017 08:56:54 PM CST ************************************************************************/#include <stdio.h>#include <stdlib.h>int main(){    int weight[1024];    int programmer[1024];    int rank[1024];    int np, ng, first_np;    int i, j, k;    int rem;    int max_tmp;    scanf("%d%d", &np, &ng);    for (i = 0; i < np; i++)        scanf("%d", weight+i);    for (i = 0; i < np; i++)        scanf("%d", programmer+i);    first_np = np;    while (1)    {        rem = np % ng;        j = 0;        for (k = 0; k < np; k++)        {            max_tmp = -1;            for (i = ng*k; i < ng*k+ng && i < np; i++)            {                if (weight[programmer[i]] > max_tmp)                    max_tmp = weight[programmer[i]];            }            for (i = ng*k; i < ng*k+ng && i < np; i++)            {                if (weight[programmer[i]] == max_tmp)                {                    programmer[j] = programmer[i];                    j++;                }                else                    rank[programmer[i]] = np / ng + (rem==0 ? 0 : 1) + 1;            }        }        if (np == 1)        {            rank[programmer[0]] = 1;             break;        }        np = np / ng + (rem==0 ? 0 : 1);    }    for (i = 0; i < first_np; i++)    {        printf("%d", rank[i]);        printf(i==first_np-1 ? "":" ");    }    printf("\n");        return 0;}