1056 Mice and Rice (25分)

在MiceandRice编程竞赛中,程序员通过编写控制鼠标移动的代码来竞争,目标是让鼠标吃尽可能多的大米。比赛通过多轮小组赛淘汰制进行,最终确定所有程序员的排名。本文介绍了一种算法,用于根据初始顺序和各鼠标重量计算程序员的最终排名。
摘要由CSDN通过智能技术生成

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 N​P​​ programmers. Then every N​G​​ 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 N​G​​ 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: N​P​​ and N​G​​ (≤1000), the number of programmers and the maximum number of mice in a group, respectively. If there are less than N​G​​ 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 N​P​​ distinct non-negative numbers W​i​​ (i=0,⋯,N​P​​−1) where each W​i​​ is the weight of the i-th mouse respectively. The third line gives the initial playing order which is a permutation of 0,⋯,N​P​​−1 (assume that the programmers are numbered from 0 to N​P​​−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;
}
/*

*/

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值