zoukankan      html  css  js  c++  java
  • A1056. Mice and Rice

    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 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

     1 #include<cstdio>
     2 #include<iostream>
     3 #include<algorithm>
     4 #include<queue>
     5 using namespace std;
     6 typedef struct node{
     7     int weight;
     8     int rank;
     9     int rank_finall;
    10 }info;
    11 info mice[100001];
    12 queue<int> qu;
    13 int rk[100001];
    14 bool cmp(int a, int b){
    15     return mice[a].rank > mice[b].rank;
    16 }
    17 int main(){
    18     int NP, NG, comp, temp;
    19     scanf("%d%d", &NP, &NG);
    20     for(int i = 0; i < NP; i++){
    21         scanf("%d", &mice[i].weight);
    22         mice[i].rank = 0;
    23         rk[i] = i;
    24     }
    25     for(int i = 0; i < NP; i++){
    26         scanf("%d", &temp);
    27         qu.push(temp);
    28     }
    29     comp = NP;  //该轮比赛的人数
    30     int rank = 1;
    31     while(comp > 1){
    32         for(int j = 0; j < comp; j += NG){
    33             int maxW = -1;
    34             int maxIndex, tempIndex;
    35             for(int k = 0; k < NG && j + k < comp; k++){  //防止最后一轮人数不足
    36                 tempIndex = qu.front();
    37                 mice[tempIndex].rank = rank;
    38                 qu.pop();
    39                 if(mice[tempIndex].weight > maxW){
    40                     maxW = mice[tempIndex].weight;
    41                     maxIndex = tempIndex;
    42                 }
    43             }
    44             qu.push(maxIndex);
    45         }
    46         rank++;
    47         comp = comp % NG == 0 ? comp / NG : comp / NG + 1;
    48     }
    49     int maxPt = qu.front();
    50     mice[maxPt].rank = rank;
    51     sort(rk, rk + NP, cmp);
    52     mice[rk[0]].rank_finall = 1;
    53     for(int i = 1; i < NP; i++){
    54         if(mice[rk[i]].rank == mice[rk[i - 1]].rank)
    55             mice[rk[i]].rank_finall = mice[rk[i - 1]].rank_finall;
    56         else mice[rk[i]].rank_finall = i + 1;
    57     }
    58     printf("%d", mice[0].rank_finall);
    59     for(int i = 1; i < NP; i++){
    60         printf(" %d", mice[i].rank_finall);
    61     }
    62     cin >> NP;
    63     return 0;
    64 }
    View Code

    总结:

    1、题意:给出NP只老鼠比体重大小,比法是:每NG个分为一组选出最大的进入下一轮,下一轮依旧每NG各分一组......直到选出最大的。题目要求按照输入的老鼠的顺序输出他们的排名(并列的老鼠名次相同,但占用排位)。另外注意给老鼠分组时,最后一组不满NG个的情况但仍然算一组。

    2、queue、stack、vector等存储的是数据的拷贝而不是引用,所以在对struct数据使用这些容器时,最好的办法是将所有的struct存储在data数组中,而将它们的数组下标作为vector等的元素,相当于存储了引用。

    3、最好使用STL的queue而非自己写一个队列。

  • 相关阅读:
    整理了所有软件,增加了一些常用的,总计约150个
    Delegate与Action,建议先看为敬
    你们要的练手项目来了
    心塞,我的配置文件到底去哪了
    Modbus,看这个就行了
    PLC做得好好的,我为什么要去学上位机?
    这种取巧的方法,你应该掌握
    从零开始实现放置游戏(十六)——道具系统(1)道具字典
    Elasticsearch 之 Filter 与 Query 有啥不同?
    【小菜学网络】MTU
  • 原文地址:https://www.cnblogs.com/zhuqiwei-blog/p/8530722.html
Copyright © 2011-2022 走看看