鱼C论坛

 找回密码
 立即注册
查看: 2145|回复: 0

[技术交流] PTA A_1056 Mice and Rice

[复制链接]
发表于 2020-2-4 17:08:26 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

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

x
传送门:https://pintia.cn/problem-sets/994805342720868352/problems/994805419468242944

解:
队列
  1. #include<cstdio>
  2. #include<queue>

  3. using namespace std;

  4. struct{
  5.     int weight, rank;
  6. }mouse[1010];

  7. int main(void)
  8. {
  9.     int np, ng;
  10.    
  11.     scanf("%d %d", &np, &ng);
  12.    
  13.     queue<int> q;
  14.    
  15.     for (int i = 0; i < np; i++)
  16.         scanf("%d", &mouse[i].weight);
  17.         
  18.     for (int i = 0; i < np; i++)
  19.     {
  20.         int order;
  21.         
  22.         scanf("%d", &order);
  23.         q.push(order);
  24.     }
  25.    
  26.     int len;
  27.     while ((len = q.size()) > 1)
  28.     {
  29.         int group = (len % ng) ? (len / ng + 1) : (len / ng);
  30.         
  31. //      printf("group = %d, ", group);
  32.         for (int i = 0; i < group; i++)
  33.         {
  34.             int winer = q.front();
  35.             
  36.             for (int j = 0; j < ng && (i * ng + j) < len; j++)
  37.             {
  38.                 mouse[q.front()].rank = group + 1;
  39.                
  40.                 if (mouse[q.front()].weight > mouse[winer].weight)
  41.                     winer = q.front();
  42.                
  43. //              printf("%d ", mouse[q.front()].weight);
  44.                 q.pop();
  45.             }
  46.             
  47.             q.push(winer);   
  48.         }
  49.         
  50. //      putchar('\n');
  51.     }
  52.    
  53.     mouse[q.front()].rank = 1;
  54.    
  55.     for (int i = 0; i < np; i++)
  56.     {
  57.         printf("%d", mouse[i].rank);
  58.         
  59.         if (i < np - 1)
  60.             putchar(' ');
  61.     }
  62.    
  63.     return 0;   
  64. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-7-1 16:21

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表