1056 Mice and Rice (25)

[复制链接]
发表于 2025-12-24 23:22:54 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
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:
  1. 11 3
  2. 25 18 0 46 37 3 19 22 57 56 10
  3. 6 0 8 7 10 5 9 1 4 2 3
复制代码
Sample Output:
  1. 5 5 5 2 5 5 5 3 1 3 5
复制代码
标题大意:np为老鼠的数目,ng为每组最多g个老鼠。先给出np个老鼠的重量,再给出老鼠的初始序次(第i名的老鼠是第j号,j从0开始)。每ng个老鼠分为一组(末了不敷np的单独为1组),对于每组老鼠,选出最重的谁人,晋级下一轮角逐,未晋级的排名为队伍数目+1,再依次再以np个老鼠一组分类,然后选出重量最大的,直到只剩下一只老鼠,排名为1。这个排名是按照原输入老鼠的序次输出老鼠的排名。
分析:模拟角逐即可。注意第一次角逐是按照给定的序次举行,之后的角逐是按照每次晋级老鼠的位置举行。
  1. #include<algorithm>
  2. #include <iostream>
  3. #include  <cstdlib>
  4. #include  <cstring>
  5. #include   <string>
  6. #include   <vector>
  7. #include   <cstdio>
  8. #include    <queue>
  9. #include    <stack>
  10. #include    <ctime>
  11. #include    <cmath>
  12. #include      <map>
  13. #include      <set>
  14. #define INF 0xffffffff
  15. #define db1(x) cout<<#x<<"="<<(x)<<endl
  16. #define db2(x,y) cout<<#x<<"="<<(x)<<", "<<#y<<"="<<(y)<<endl
  17. #define db3(x,y,z) cout<<#x<<"="<<(x)<<", "<<#y<<"="<<(y)<<", "<<#z<<"="<<(z)<<endl
  18. #define db4(x,y,z,r) cout<<#x<<"="<<(x)<<", "<<#y<<"="<<(y)<<", "<<#z<<"="<<(z)<<", "<<#r<<"="<<(r)<<endl
  19. using namespace std;
  20. typedef struct
  21. {
  22.     int weight,ind;
  23. }mice;
  24. int main(void)
  25. {
  26.     #ifdef test
  27.     freopen("in.txt","r",stdin);
  28.     //freopen("in.txt","w",stdout);
  29.     clock_t start=clock();
  30.     #endif //test
  31.     int np,ng;scanf("%d%d",&np,&ng);
  32.     mice num[np],cnt[np];
  33.     int ans[np]={0},que[np]={0};
  34.     int t=np;
  35.     for(int i=0;i<np;++i)
  36.         scanf("%d",&num[i].weight),num[i].ind=i;
  37.     for(int i=0;i<np;++i)
  38.         scanf("%d",&que[i]);
  39.     int tt=0;
  40.     int ind=0,val;
  41.     if(t%ng!=0)val=t/ng+2;
  42.     else val=t/ng+1;
  43.     for(int i=0;i<t;i=i+ng)
  44.     {
  45.         int temp=num[que[i]].weight,index=i,r=min(i+ng,t);
  46.         for(int j=i+1;j<r;++j)
  47.         {
  48.             if(num[que[j]].weight>temp)temp=num[que[j]].weight,index=j;
  49.         }
  50.         for(int j=i;j<r;++j)
  51.         {
  52.             if(j!=index)ans[num[que[j]].ind]=val;
  53.         }
  54.         cnt[tt]=num[que[index]];
  55.         tt++;
  56.     }
  57.     t=tt,tt=0;
  58.     for(int i=0;i<t;++i)
  59.         num[i]=cnt[i];
  60.     while(t>1)
  61.     {
  62.         ind=0;
  63.         if(t%ng!=0)val=t/ng+2;
  64.         else val=t/ng+1;
  65.         for(int i=0;i<t;i=i+ng)
  66.         {
  67.             int temp=num[i].weight,index=i,r=min(i+ng,t);
  68.             for(int j=i+1;j<r;++j)
  69.             {
  70.                 if(num[j].weight>temp)temp=num[j].weight,index=j;
  71.             }
  72.             for(int j=i;j<r;++j)
  73.             {
  74.                 if(j!=index)ans[num[j].ind]=val;
  75.             }
  76.             cnt[tt]=num[index];
  77.             tt++;
  78.         }
  79.         for(int i=0;i<tt;++i)
  80.             num[i]=cnt[i];
  81.         t=tt,tt=0;
  82.     }
  83.     ans[num[0].ind]=1;
  84.     for(int i=0;i<np;++i)
  85.     {
  86.         if(!i)printf("%d",ans[i]);
  87.         else printf(" %d",ans[i]);
  88.     }
  89.     printf("\n");
  90.     #ifdef test
  91.     clockid_t end=clock();
  92.     double endtime=(double)(end-start)/CLOCKS_PER_SEC;
  93.     printf("\n\n\n\n\n");
  94.     cout<<"Total time:"<<endtime<<"s"<<endl;        //s为单位
  95.     cout<<"Total time:"<<endtime*1000<<"ms"<<endl;    //ms为单位
  96.     #endif //test
  97.     return 0;
  98. }
复制代码
回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表