• pat 甲级 1056. Mice and Rice (25)


    1056. Mice and Rice (25)

    时间限制
    100 ms
    内存限制
    65536 kB
    代码长度限制
    16000 B
    判题程序
    Standard
    作者
    CHEN, Yue

    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

    题意:模拟,设最多x个人一组,每一轮游戏先按照游玩者玩游戏的顺序分组,每一组产生一名分数最高者,其余的人止步这一轮比赛并获得(下一轮游玩人数)+1的名次,直至决定出第一名为止。
    AC代码:
    #define _CRT_SECURE_NO_DEPRECATE
    #include<iostream>
    #include<algorithm>
    #include<cmath>
    #include<cstring>
    #include<string>
    #include<set>
    #include<queue>
    using namespace std;
    #define INF 0x3f3f3f
    #define N_MAX 100000+5
    int n, group;
    int weight[N_MAX],Rank[N_MAX];
    vector<int>permute;
    int main() {
        while (cin >> n >> group) {
            for (int i = 0; i < n; i++)scanf("%d", &weight[i]);
            for (int i = 0; i < n; i++) { int a; scanf("%d", &a); permute.push_back(a); }
            vector<int>vec = permute, vec2; int N = vec.size();
            while (N > group) {
                int rank = (N%group == 0) ? N / group + 1 : N / group + 2;
                int i = 0, tmpi = group;
                while (1) {
                    int max_wei = 0, id;
                    for (; i < tmpi; i++) {
                        if (max_wei < weight[vec[i]]) {
                            max_wei = weight[vec[i]];
                            id = vec[i];
                        }
                    }
                    vec2.push_back(id); //没落选的    
                    for (int k = tmpi - group; k < tmpi; k++)//落选的确定排名
                        if (vec[k] != id)Rank[vec[k]] = rank;
                    if (tmpi == N)break;
                    tmpi += group;
                    if (tmpi > N)tmpi = N;
                }
                vec = vec2;
                N = vec.size();
                vec2.clear();
            }
            if (N > 0) {//最后一组
                int max_wei = 0,id;
                for (int i = 0; i < vec.size();i++) {
                    if (max_wei < weight[vec[i]]) {
                        max_wei = weight[vec[i]];
                        id = vec[i];
                    }
                }
                for (int i = 0; i < vec.size();i++) {
                    if (vec[i] != id)Rank[vec[i]] = 2;
                    else Rank[vec[i]] = 1;
                }
            }
    
            for (int i = 0; i < n; i++)
                printf("%d%c", Rank[i], i + 1 == n ? '
    ' : ' ');
        }
    
        return 0;
    }
  • 相关阅读:
    Redis笔记
    java多线程 interrupt(), interrupted(), isInterrupted()方法区别
    策略模式
    外观模式
    Java线程池原理与架构分析
    状态模式
    模板方法模式
    LeetCode | Path-Sum
    实用工具
    IDEA springboot配置
  • 原文地址:https://www.cnblogs.com/ZefengYao/p/8536872.html
Copyright © 2020-2023  润新知