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 }

转载于:https://www.cnblogs.com/Deribs4/p/4773715.html

pat1056. Mice and Rice (25)相关推荐

  1. 1056. Mice and Rice (25)

    1056. Mice and Rice (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Mice an ...

  2. 1056 Mice and Rice (25分)

    1056 Mice and Rice (25分) 参考 题目读了好久都没读明白...看了上方链接的解释才懂什么意思,感觉好绕... 第二行为0~n-1的老鼠重量,第三行为合并的次序序号,最后按0到n- ...

  3. A1056 Mice and Rice (25 分| queue用法,附详细注释,逻辑分析)

    写在前面 思路分析 np为老鼠数量, ng为每组最多g个老鼠. 先给出np个老鼠重量,再给出老鼠初始顺序 每ng个老鼠分为1组,对于每组老鼠,选出最重的那个,晋级下1轮比赛,然后依次再以np个老鼠1组 ...

  4. 1056 Mice and Rice (25 point(s))

    Matter: 1.很难受,这个想了好长时间,发现没读懂题目.second line 是每个老鼠的质量(W​i​​ is the weight of the i-th mouse),third lin ...

  5. 1056 Mice and Rice (25 分)【难度: 一般 / 知识点: 模拟】

    https://pintia.cn/problem-sets/994805342720868352/problems/994805419468242944 这里是用队列模拟. #include< ...

  6. PTA 1056 Mice and Rice (25分)

    题面:自己读,甲级题面自己读; 神奇的做法 一开始以为就是一个堆栈的模拟 没考虑到复杂度写到一半发现 查找中位数的复杂度实在是太高了O(nnlog) 那不就裂开了嘛 后来想到 中位数 可以单独维护一个 ...

  7. PAT 甲级 1056 Mice and Rice

    1056 Mice and Rice 题目大意:给出老鼠的数量和每组的人数,将老鼠分成若干组(多下来不够每组人数的算作一组),第二行给出每只老鼠最终的重量,第三行给出分组顺序 思路:记录每轮获胜的选手 ...

  8. PAT 1056 Mice and Rice

    for the sake of simplicity:为了简单起见 permutation:排序 Np programmers:the number of programmers(程序员的数量) Ng ...

  9. PAT甲级1056 Mice and Rice:[C++题解]模拟、排名

    文章目录 题目分析 题目来源 题目分析 来源:acwing 分析: 本题采用vector存每只老鼠,为什么用vector,因为每过一轮剩余老鼠数量是动态变化的. 每组中未选出的选手排名相同,排名等于进 ...

  10. 1056 Mice and Rice

    题目 题意: n为老鼠的数量,每组最多m个老鼠.先给出n个老鼠的重量,再给出老鼠的初始顺序(第i名的老鼠是第j号,j从0开始).每m个老鼠分为一组,对于每组老鼠,选出最重的那个,晋级下一轮比赛,然后依 ...

最新文章

  1. 优达学城《DeepLearning》项目2:犬种分类器
  2. php base64解码图片 base64加密图片还原
  3. 我们是如何使用 PingCode Flow 实现研发自动化管理的?
  4. Hibernate之HQL数据库操作
  5. datagrid如何获取一行数据中的某个字段值_使用Mysql 数据库 新手常见问题
  6. 安装SeleniumPhantomJS
  7. Server.MapPath(path)的使用
  8. Android中关于setLatestEventInfo()过时以及构建Notification的解决方法
  9. C++11 auto和decltype关键字
  10. 基本定时器TIM6和TIM7使用
  11. python模块导入视频教程_63-知识点回顾-函数和导入模块
  12. 在linux下做源码免杀,Cobaltstrike免杀从源码级到落地思维转变
  13. python运用maya_Maya中Python脚本的使用(一)
  14. 计算机系统的优化项目,计算机系统项目集成管理问题与对策
  15. ZOJ-1010 奇偶剪枝
  16. python贪心算法几个经典例子_Python笔试——贪心算法
  17. 高频电子线路复习笔记(2)——高频电路基础
  18. Anaconda安装教程
  19. 裸金属服务器启动之PXE与IPXE实践
  20. Echarts实现中国地图线路图特效(一对多发射点)

热门文章

  1. Tips of keras
  2. Prompt Learning | 一文带你概览Prompt工作新进展
  3. 【ICLR2021必读】 【自监督学习】 【Transformer】相关论文
  4. 【GNN】图表示学习Graph Embedding综述
  5. BERT源码分析(一)
  6. 是时候研读一波导师的论文--一个简单有效的联合模型
  7. 每日算法系列【LeetCode 907】子数组的最小值之和
  8. [ICLR18]联合句法和词汇学习的神经语言模型
  9. matplotlib-plt.title
  10. 为什么选择Netty作为基础通信框架?