1056. 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 NGprogrammers 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

题目大意:这是一个比赛中不断分组最终决出胜者的题目。首先给出选手数量np和每组人数ng,然后按序号顺序给出每个选手的分数,接着是以选手序号组成的比赛顺序,按照这个顺序把选手分成不同组,每组有一个优胜者加入下一轮并重复上述分组,而失败者拥有相同的排名并被淘汰。要求输出最终所有选手的排名。



主要思想:思路很简单,利用循环来模拟每轮的比赛,首先需要求出当前轮的组数,然后对于每组成员找出得分最高的选手并将其加入下一轮,将失败者的排名设置为当前组数+1。其中重点是如何更新下一轮的比赛顺序,这里用的是vector容器来储存比赛顺序,将每一组胜者直接覆盖在数组的前端,而下一轮比赛的选手数就是此轮的组数。

#include <iostream>
#include <vector>
using namespace std;
int weight[1000]; //选手得分
int rank_id[1000]; //选手排名
vector<int> order; //比赛顺序
int get_max(int lo, int hi, int p);
int min(int a, int b); int main(void) {
int np, ng, i; cin >> np >> ng;
for (i = 0; i < np; i++) {
cin >> weight[i];
}
for (i = 0; i < np; i++) {
int t;
cin >> t;
order.push_back(t);
}
int n = np;
while (true) {
int groups = n / ng; //n是当前轮比赛的人数
int r = n % ng;
if (r > 0) groups++; //求出分组数
int lo = 0, count = 0;
int win_id;
for (i = 0; i < groups; i++) {
int hi = min(lo+ng-1, n-1);
win_id = get_max(lo, hi, groups); //该组的优胜者序号
order[count++] = win_id; //不断更新下一轮的
lo = hi+1;
}
n = groups;
if (n == 1) {
rank_id[win_id] = 1;
break;
}
}
cout << rank_id[0];
for (i = 1; i < np; i++)
cout << " " << rank_id[i];
cout << endl; return 0;
} //获得当前组优胜选手的序号,并且把失败者排名设置为group+1
int get_max(int lo, int hi, int p) {
int max_id, i;
int max = 0;
for (i = lo; i <= hi; i++) {
if (max < weight[order[i]]){
max = weight[order[i]];
max_id = order[i];
}
}
for (i = lo; i <= hi; i++) {
if (order[i] != max_id)
rank_id[order[i]] = p + 1;
} return max_id;
}
int min(int a, int b) {
return a < b ? a : b;
}

PAT-1056 Mice and Rice (分组决胜问题)的更多相关文章

  1. PAT 1056 Mice and Rice[难][不理解]

    1056 Mice and Rice(25 分) Mice and Rice is the name of a programming contest in which each programmer ...

  2. PAT 1056 Mice and Rice

    #include <cstdio> #include <climits> #include <cstdlib> #include <vector> #i ...

  3. pat 甲级 1056. Mice and Rice (25)

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

  4. PAT 甲级 1056 Mice and Rice (25 分) (队列,读不懂题,读懂了一遍过)

    1056 Mice and Rice (25 分)   Mice and Rice is the name of a programming contest in which each program ...

  5. PAT Advanced 1056 Mice and Rice (25) [queue的⽤法]

    题目 Mice and Rice is the name of a programming contest in which each programmer must write a piece of ...

  6. 1056 Mice and Rice (25分)队列

    1.27刷题2 Mice and Rice is the name of a programming contest in which each programmer must write a pie ...

  7. 1056. Mice and Rice (25)

    时间限制 30 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Mice and Rice is the name of a pr ...

  8. PAT甲题题解-1056. Mice and Rice (25)-模拟题

    有n个老鼠,第一行给出n个老鼠的重量,第二行给出他们的顺序.1.每一轮分成若干组,每组m个老鼠,不能整除的多余的作为最后一组.2.每组重量最大的进入下一轮.让你给出每只老鼠最后的排名.很简单,用两个数 ...

  9. PAT (Advanced Level) 1056. Mice and Rice (25)

    简单模拟. #include<iostream> #include<cstring> #include<cmath> #include<algorithm&g ...

  10. 【PAT甲级】1056 Mice and Rice (25 分)

    题意: 输入两个正整数N和M(<=1000),接着输入两行,每行N个数,第一行为每只老鼠的重量,第二行为每只老鼠出战的顺序.输出它们的名次.(按照出战顺序每M只老鼠分为一组,剩余不足M只为一组, ...

随机推荐

  1. jax-rs下载文件

    @Path("/file") public class FileService { private static final String FILE_PATH = "c: ...

  2. 微软开放 Build 2020 免费注册

    微软已经开放 Build 2020 线上开发者会议注册,https://mybuild.microsoft.com/.Build 2020 会议将于 5 月 19 日至 20 日召开,核心内容都是围绕 ...

  3. HTML 教程之常用html标签

    前端三把利器: HTML:赤裸裸的人 20个标签 CSS:华丽的衣服  颜色 位置 …… JS:让这个人动起来 一.HTML本质及在web程序中的作用 web访问中,浏览器充当一个socket客户端. ...

  4. CYQ.Data 轻量数据层之路 使用篇-MProc 存储过程与SQL 视频[最后一集] H (二十八)

    2019独角兽企业重金招聘Python工程师标准>>> 说明: 本次录制主要为使用篇:CYQ.Data 轻量数据层之路 使用篇五曲 MProc 存储过程与SQL(十六)   的附加视 ...

  5. 百度云BaaS体系揭秘,突破共识机制、单机计算和串行处理三大瓶颈

    区块链作为去中心化的技术机制拥有广泛的应用场景与市场潜能.自2017年爆发式增长后,区块链虽然已经进入平稳期,但仍然存在概念混淆.技术性能制约.智能合约制约.共识机制.网络建设等痛点.为了打破行业壁垒 ...

  6. AMD 开源照片级渲染引擎 Radeon ProRender

    除了Radeon Pro WX.Radeon Pro Solid State两款全新的专业显卡,AMD今天还宣布,Radeon ProRender渲染引擎即将开放源代码,开发人员可任意使用.AMD去年 ...

  7. Codeforces Round #623 (Div. 2, based on VK Cup 2019-2020 - Elimination Round, Engine) A Dead Pixel

    讨论坏点的左右上下的矩形大小. #include <bits/stdc++.h> using namespace std; int main() { int t; cin >> ...

  8. P3842 [TJOI2007]线段

    最近多刷些dp,觉得这个算不上蓝题   在一个\(n\times n\)的平面上,在每一行中有一条线段,第\(i\)行的线段的左端点是\((i, L_i)\),右端点是\((i, R_i)\),其中\ ...

  9. PHP命令执行学习总结

    前言 最近学习了PHP命令执行,内容比较多,把自己学到的总结下来,加深理解,水平有限,欢迎大佬斧正. 什么是PHP命令注入攻击? Command Injection,即命令注入攻击,是指由于Web应用 ...

  10. MySQL——视图/触发器/事务/存储过程/函数/流程控制

    一 视图 视图是一个虚拟表(非真实存在),其本质是[根据SQL语句获取动态的数据集,并为其命名],用户使用时只需使用[名称]即可获取结果集,可以将该结果集当做表来使用. 使用视图我们可以把查询过程中的 ...