pat1056. Mice and Rice (25)
1056. Mice and Rice (25)
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 就可以了。
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<queue>
#include<vector>
#include<cmath>
#include<string>
#include<map>
#include<set>
using namespace std;
int ra[],weight[],order[];
int main(){
//freopen("D:\\INPUT.txt","r",stdin);
int np,ng;
scanf("%d %d",&np,&ng);
int i,j;
for(i=;i<np;i++){
scanf("%d",&weight[i]);
}
for(i=;i<np;i++){
scanf("%d",&order[i]);
} int nnp=np;
int r=nnp/ng+(nnp%ng==?:)+;//每局的等级;
int s,maxnum;
queue<int> q;//存放编号
for(i=;i<nnp;){
ra[order[i]]=r;
maxnum=order[i];//每一轮都有最大值
s=i++;
for(;i<nnp&&i-s<ng;i++){
ra[order[i]]=r;
if(weight[maxnum]<weight[order[i]]){
maxnum=order[i];
}
}
q.push(maxnum);//每组的最大值进入到下一次比赛中
//cout<<maxnum<<endl;
}
while(q.size()>){
nnp=q.size();
r=nnp/ng+(nnp%ng==?:)+;
for(i=;i<nnp;){
maxnum=q.front();
q.pop();
ra[maxnum]=r;
s=i++;
for(;i<nnp&&i-s<ng;i++){
ra[q.front()]=r;
if(weight[maxnum]<weight[q.front()]){
maxnum=q.front();
}
q.pop();//不要忘
}
q.push(maxnum);
}
}
ra[q.front()]=;
printf("%d",ra[]);
for(i=;i<np;i++){
printf(" %d",ra[i]);
}
return ;
}
pat1056. Mice and Rice (25)的更多相关文章
- PAT1056:Mice and Rice
1056. Mice and Rice (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Mice an ...
- pat 甲级 1056. Mice and Rice (25)
1056. Mice and Rice (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Mice an ...
- 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 ...
- 1056. Mice and Rice (25)
时间限制 30 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Mice and Rice is the name of a pr ...
- 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 ...
- PAT-1056 Mice and Rice (分组决胜问题)
1056. Mice and Rice Mice and Rice is the name of a programming contest in which each programmer must ...
- 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 ...
- PAT甲题题解-1056. Mice and Rice (25)-模拟题
有n个老鼠,第一行给出n个老鼠的重量,第二行给出他们的顺序.1.每一轮分成若干组,每组m个老鼠,不能整除的多余的作为最后一组.2.每组重量最大的进入下一轮.让你给出每只老鼠最后的排名.很简单,用两个数 ...
- PAT (Advanced Level) 1056. Mice and Rice (25)
简单模拟. #include<iostream> #include<cstring> #include<cmath> #include<algorithm&g ...
随机推荐
- JVM体系结构之三:方法区之1
一.简介 方法区在JVM中也是一个非常重要的区域,它与堆一样,是被线程共享的区域.在方法区中,存储了每个类的信息(包括类的名称.方法信息.字段信息).静态变量.常量以及编译器编译后的代码等. 方法区( ...
- ss3
1)执行如下命令,进行安装shadowsocks(命令需要一条条的输入,然后回车,执行完一条,在执行下一条命令),过程中可能会让你选择确认的地方,直接输入"y"回车即可: yum ...
- 问题:C#调webservice超时;结果:C#调用webservice服务超时
C#调用WebService服务时,报错,The operation has timed out,意思是“操作超时”. 方法/步骤 首先修改服务端配置 WebService服务所在站点为服务端,它提供 ...
- eclipse中使用Maven插件报错:-Dmaven.multiModuleProjectDirectory system property is not set. Check $M2_HOME environment variable and mvn script match.
步骤: 1.添加M2_HOME的环境变量 2.Preference->Java->Installed JREs->Edit 选择一个jdk 3.添加 -Dmaven.multiMod ...
- assert.fail()
assert.fail(message) assert.fail(actual, expected[, message[, operator[, stackStartFunction]]]) oper ...
- leetcode:234. Palindrome Linked List
这个题目非常好.http://blog.csdn.net/u012249528/article/details/47124771给出了三种解法,其中前两个是不满足条件的,不过具有参考价值: 第一种办法 ...
- CSS学习系列1 - CSS中的盒子模型 box model
css中有一个盒子模型的概念. 主要是用来告诉浏览器如何来计算页面元素的宽度和高度, 比如该元素的宽度/高度 是否包括内边距,边框,外边距. 盒子模型有一个属性box-sizing属性来说明是否包括 ...
- 3. 文件上传靶机实战(附靶机跟writeup)
upload-labs 一个帮你总结所有类型的上传漏洞的靶场 文件上传靶机下载地址:https://github.com/c0ny1/upload-labs 运行环境 操作系统:推荐windows ...
- 从 .NET Framework到 .NET Core
参考资料: https://docs.microsoft.com/zh-cn/dotnet/core/porting/ https://docs.microsoft.com/zh-cn/aspnet/ ...
- Spring Security 入门原理及实战
目录 从一个Spring Security的例子开始 创建不受保护的应用 加入spring security 保护应用 关闭security.basic ,使用form表单页面登录 角色-资源 访问控 ...