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 ...
随机推荐
- java core
1: Java7 以后的 NIO. 2: 泛型要掌握,这里重点强调一点,泛型类之间不存在继承关系,所有的泛型对象在编译后都会去泛型化,都是同一个 class 对象,例如 ArrayList< ...
- java处理中日文字符串的乱码问题
——杂言:前段时间在处理音频预览问题,详见关于audiojs的研究.期间,将远端的音频下载并缓存在本地过程中,涉及到java.io.*的几个操作,发生一些乱码问题. 我以前的处理是将本地的编码转换为U ...
- mongodb切换到admin
127.0.0.1:后面是端口,/admin是切换到管理员权限 cd C:\Program Files\MongoDB\Server\3.0\bin mongo.exe 127.0.0.1:27017 ...
- SpringSecurity01 SpringSecurity环境搭建
版本说明: JDK -> java version "1.8.0_101" MAVEN -> Apache Maven 3.5.0 IDEA -> 2017.2. ...
- CodeForces 492C Vanya and Exams (贪心)
C. Vanya and Exams time limit per test 1 second memory limit per test 256 megabytes input standard i ...
- 【机器学习】推荐系统、SVD分解降维
推荐系统: 1.基于内容的实现:KNN等 2.基于协同滤波(CF)实现:SVD → pLSA(从LSA发展而来,由SVD实现).LDA.GDBT SVD算是比较老的方法,后期演进的主题模型主要是pLS ...
- PyQt中从RAM新建QIcon对象 / Create a QIcon from binary data
一般,QIcon是通过png或ico等图标文件来初始化的,但是如果图标资源已经在内存里了,或者一个zip压缩文件内,可以通过QPixmap作为桥梁,转换为图标. zf = zipfile.ZipFil ...
- CSS概念 - 可视化格式模型(一) 盒模型与外边距叠加
可以参考<精通CSS 高级WEB标准解决方案>第三章. 可视化格式模型 可视化格式模型要掌握的3个最重要的CSS概念是 浮动.定位.盒模型. 这些概念控制在页面上安排和显示元素的方式, 形 ...
- HN669打包工具--打包工具使用文档
打包工具主要包含下载更新资源文件以及打包两个部分 一.下载更新资源文件 1.终端进入工具根目录,即HN669SDK_iOS目录,并运行./api.sh脚本 2.输入游戏id (note:此游戏id为我 ...
- 【linux下-远程访问mysql数据库报错问题】
虚拟机跑Linux项目用到MySQL数据库,可是远程连接MySQL时总是报出erro 2003: Can't connect to MySQL server on '211.87.***.***' ( ...