• pat1056. Mice and Rice (25)


    1056. Mice and Rice (25)

    时间限制
    30 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 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.题意理解问题。题目并没有每次等级的计算公式,但网上的说法是rank=np/ng+(np%ng==0?0:1)+1。不知道为什么,我还以为先分出等级,然后由上到下,每一等级由大到小编号。这里时间耗费较多。

    2.代码书写。由于等级的计算公式不是理解,所以写代码开始逻辑并不清楚。本来想先每ng处理,然后最后剩下的单独处理,后来一想,其实只要加判断条件 i<nnp 就可以了。

     1 #include<cstdio>
     2 #include<algorithm>
     3 #include<iostream>
     4 #include<cstring>
     5 #include<queue>
     6 #include<vector>
     7 #include<cmath>
     8 #include<string>
     9 #include<map>
    10 #include<set>
    11 using namespace std;
    12 int ra[1005],weight[1005],order[1005];
    13 int main(){
    14     //freopen("D:\INPUT.txt","r",stdin);
    15     int np,ng;
    16     scanf("%d %d",&np,&ng);
    17     int i,j;
    18     for(i=0;i<np;i++){
    19         scanf("%d",&weight[i]);
    20     }
    21     for(i=0;i<np;i++){
    22         scanf("%d",&order[i]);
    23     }
    24 
    25 
    26     int nnp=np;
    27     int r=nnp/ng+(nnp%ng==0?0:1)+1;//每局的等级;
    28     int s,maxnum;
    29     queue<int> q;//存放编号
    30     for(i=0;i<nnp;){
    31         ra[order[i]]=r;
    32         maxnum=order[i];//每一轮都有最大值
    33         s=i++;
    34         for(;i<nnp&&i-s<ng;i++){
    35             ra[order[i]]=r;
    36             if(weight[maxnum]<weight[order[i]]){
    37                 maxnum=order[i];
    38             }
    39         }
    40         q.push(maxnum);//每组的最大值进入到下一次比赛中
    41         //cout<<maxnum<<endl;
    42     }
    43     while(q.size()>1){
    44         nnp=q.size();
    45         r=nnp/ng+(nnp%ng==0?0:1)+1;
    46         for(i=0;i<nnp;){
    47             maxnum=q.front();
    48             q.pop();
    49             ra[maxnum]=r;
    50             s=i++;
    51             for(;i<nnp&&i-s<ng;i++){
    52                 ra[q.front()]=r;
    53                 if(weight[maxnum]<weight[q.front()]){
    54                     maxnum=q.front();
    55                 }
    56                 q.pop();//不要忘
    57             }
    58             q.push(maxnum);
    59         }
    60     }
    61     ra[q.front()]=1;
    62     printf("%d",ra[0]);
    63     for(i=1;i<np;i++){
    64         printf(" %d",ra[i]);
    65     }
    66     return 0;
    67 }
  • 相关阅读:
    MYSQL limit用法
    mybaties mapping中if
    执行数据库同时又调接口
    WITH (NOLOCK)
    SpringMVC转发和重定向区别!
    MyBatis的foreach语句详解
    SSM mapper.xml
    win7与virtualbox中centos文件共享
    PBOC2.0中消费交易流程
    PBOC2.0协议中电子存折/电子钱包中圈存交易流程
  • 原文地址:https://www.cnblogs.com/Deribs4/p/4773715.html
Copyright © 2020-2023  润新知