PAT甲级1056
原题:
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、题目虽然说是非负数,但实际上都是整数,且没有重复,可以在数组末尾设置一个负数作为判断点
2、分组要用到ceil函数
3、要记录每轮队列中最后一个选手,以便判断该轮结束
4、排名从组数+1开始递减
5、最后一个也就是第一名要单独判断,不然输出的是2
6、用队列进行模拟,赢家继续进队列,输的踢掉
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
#include <math.h>
using namespace std;
int main(){
int np,ng; //np总人数,ng每组最大人数
scanf("%d%d",&np,&ng);
vector<int> a(np+1),b(np); //a每个人的序号,b每个人的排名
queue<int> q;
for(int i=0;i<np;i++) scanf("%d",&a[i]);
a[np]=-1; //哨兵
for(int i=0;i<np;i++){ //所有选手按分组序列进队列
int tmp;
scanf("%d",&tmp);
q.push(tmp);
}
while(!q.empty()){
bool flag=false; //记录每轮结束
int t=q.back(),l=q.size(),length=ceil(l*1.0/ng); //length记录分组情况
for(int i=0;i<length;i++){
int max=np; //记录每轮赢家
for(int j=0;j<ng;j++){
if(a[q.front()]>a[max]) max=q.front();
b[q.front()]=length+1;
if(q.front()==t){
q.pop();
flag=true; //该轮已结束
break;
}
q.pop();
}
if(l!=1) q.push(max); //当队列就剩最后一个时就不用继续处理了
else b[max]=1; //将最终排名矫正
if(flag) break;
}
}
for(int i=0;i<np;i++){
printf("%d",b[i]);
if(i!=np-1) printf(" ");
}
system("pause");
return 0;
}