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
思路:
维护一个queue,每轮按照m去卡每一组,que.size()%m如果等于0则当前轮要决出que.size()/m个老鼠,如果que.size()%m如果不等于0则当前轮要决出que.size()/m+1个老鼠,而剩下老鼠rank就是决出老鼠的数量+1。
在这个过程中将每m个的最大的那只加队伍后面。作为下一轮。直到只剩一只。
AC代码:
//#include <bits/stdc++.h>
#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <stack>
#include <time.h>
#include <string.h>
#include <vector>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <functional>
#define sdddd(x,y,z,k) scanf("%d%d%d%d", &x, &y, &z, &k)
#define sddd(x,y,z) scanf("%d%d%d", &x, &y, &z)
#define sdd(x,y) scanf("%d%d", &x, &y)
#define sd(x) scanf("%d", &x)
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define per(i,a,b) for(int i=a;i>=b;i--)
//#define mp Debug(x) printf("%d\n", &x);
#define pb push_back
#define ms(x, y) memset(x, y, sizeof x)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll MOD = 1046513837;
const int maxn = 1e5 + 50;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3f;
//typedef vector<ll> vec;
//typedef vector<vec> mat;
template <class T>
inline bool scan_d(T &ret) {
char c; int sgn;
if (c = getchar(), c == EOF) return 0;
while (c != '-' && (c<'0' || c>'9')) c = getchar();
sgn = (c == '-') ? -1 : 1;
ret = (c == '-') ? 0 : (c - '0');
while (c = getchar(), c >= '0'&&c <= '9') ret = ret * 10 + (c - '0');
ret *= sgn;
return 1;
}
struct Mos{
int num;
int weight;
};
int arr[maxn];
int ans[maxn];
queue<Mos> que;
int main() {
int n, m, tmp;
cin >> n >> m;
rep(i, 0, n-1){
scanf("%d", &arr[i]);
}
rep(i, 1, n){
scanf("%d", &tmp);
que.push({tmp, arr[tmp]});
}
int cnt;
int turnmx = -INF, inx = -1;
while(que.size()){
if(que.size() == 1){
auto tt = que.front();
ans[tt.num] = 1;
break;
}
int rnk = que.size() / m + 1;
if(que.size()%m) rnk++;
tmp = que.size();
cnt = 0;
rep(i, 1, tmp){
auto nn = que.front(); que.pop();
ans[nn.num] = rnk;
if(nn.weight > turnmx){
turnmx = nn.weight;
inx = nn.num;
}
cnt++;
if(cnt == m || i == tmp){
que.push({inx, turnmx});
turnmx = -INF, inx = -1;
cnt = 0;
}
}
}
for(int i = 0 ; i < n; ++i){
printf("%d%c", ans[i], (i==n-1)?'\n':' ');
}
return 0;
}
/*
*/