http://pat.zju.edu.cn/contests/pat-a-practise/1056
Mice and Rice are the name of a programming contest in which each programmer must write a piece of code to control the move Ments of a mouse in a given map. The goal of each mouse are to eat as much rice as possible in order to become a fatmouse.
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 of the losers in this turn are ranked the same. Every ngwinners are then grouped in the next match until a final winner is determined.
For the sake of simplicity, assume this weight of each mouse is fixed once the programmer submits code. Given the weights of 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 the contains 2 positive INTEGERS:NP and NG (<= 1000), the number of programmers and the maxi Mum number of mice in a group, respectively. If there are less than NG mice at the end of the player's ' s list, then all the mice-left would be put into the last group. The second line contains NP distinct non-negative numbers Wi (i=0,... NP-1) where each Wiis 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 the programmers are numbered from 0 to NP-1). All of the numbers in a line are separated by a.
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 the numbers must are separated by a spaces, with no extra spaces At the "line." Sample Input:
3
0 (3) 6 0 8 7 10 5 9 1 4 2 3
Sample Output:
5 5 5 2 5 5 5 3 1 3 5
#include <cstdio>
#include <queue>
using namespace std;
#define MAX 1001
int W[max];
int Ans[max];
int main () {
//freopen ("g:\\input.in", "R", stdin);
int n, m;
scanf ("%d%d", &n, &m);
for (int i = 0; i < n; i++)
scanf ("%d", &w[i]);
Queue<int> order;
int x;
for (int i = 0; i < n; i++) {
scanf ("%d", &x);
Order.push (x);
}
while (Order.size ()!= 1) {
queue<int> tmp;
int groupnum = Order.size ()/M + (order.size ()%m = = 0? 0:1);
for (int i = 0; i < groupnum i++) {
int max =-1, Maxindex =-1;
for (int j = I*m J < i*m + M && order.size ()!= 0; j +) {
int t = Order.front ();
Ans[t] = groupnum + 1;
Order.pop ();
if (W[t] > max) {
max = w[t];
Maxindex = t;
}
}
Tmp.push (Maxindex);
}
order = tmp;
}
Ans[order.front ()] = 1;
for (int i = 0; i < n; i++)
printf ("%d%c", Ans[i), (i-n+1?) ': ' \ n '));
return 0;
}