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

 

首先,这道题需要读懂题。

同样的代码在pat和牛客网运行结果不同,发现牛客网的时限是1秒,而pat是100ms,所以pat运行超时是正常的;在pat上尽量不要使用vector,经常遇到超时的情况。

这是一道模拟竞赛题,使用vector的理念非常简单,就是将胜出的都放入一个新的vector里,然后对新的vector进行同样的模拟规则。

如果不用vector,就要好好想想怎么做了,这道题关键不在谁胜出了,而是谁没有胜出,对于没有胜出的给予名次,做好标记,不再查询。

这里附上了两种实现方法。

对于使用while循环,我们尽量将跳出循环的条件判断放入while循环中,这样更一目了然,也不容易犯错误。

 

#include <bits/stdc++.h>

using namespace std;

int P, G;
int weight[1003];
vector<int> mouseVec, mouseVec2;
int ranks[10003];

int main()
{
    cin>>P>>G;
    for(int i = 0; i < P; i++) {
        scanf("%d", &weight[i]);
    }
    int x;
    for(int i = 0; i < P; i++) {
        scanf("%d", &x);
        mouseVec.push_back(x);
    }
    while(1) {
        int k, mouseMax;
        for(int i = 0; i < mouseVec.size(); i++) {
            if(i%G == 0) {
                mouseMax = weight[mouseVec[i]];
                k = mouseVec[i];
            }
            else {
                if(mouseMax < weight[mouseVec[i]]) {
                    ranks[k] = -1;
                    mouseMax = weight[mouseVec[i]];
                    k = mouseVec[i];
                }
                else {
                    ranks[mouseVec[i]] = -1;
                }
                if(i%G == G-1) {
                    mouseVec2.push_back(k);
                }
            }
            if(i == mouseVec.size()-1 && i%G != G-1) {
                mouseVec2.push_back(k);
            }
        }
        int r = mouseVec2.size();
        //cout<< "R:"<< r<< endl;
        for(int i = 0; i < mouseVec.size(); i++) {
            if(ranks[mouseVec[i]] == -1) {
                ranks[mouseVec[i]] = r+1;
                //cout<< mouseVec[i]<< " ";
            }
        }
        //cout<< endl;
        if(r == 1) {
            ranks[mouseVec2[0]] = 1;
            break;
        }
        mouseVec.clear();
        for(int i = 0; i < mouseVec2.size(); i++) {
            mouseVec.push_back(mouseVec2[i]);
        }
        mouseVec2.clear();
    }
    for(int i = 0; i < P; i++) {
        if(i != 0) printf(" ");
        printf("%d", ranks[i]);
    }
    return 0;
}

 

#include <bits/stdc++.h>

using namespace std;

int P, G;
int weight[1003];
//vector<int> mouseVec, mouseVec2;
int ranks[1003];
int order[1003];

int main()
{
    cin>>P>>G;
    for(int i = 0; i < P; i++) {
        scanf("%d", &weight[i]);
    }
    int x;
    for(int i = 0; i < P; i++) {
        scanf("%d", &order[i]);
    }
    int c = 0;
    while(1) {
        int ran = (P-c)/G+1;
        if((P-c)%G != 0) {
            ran++;
        }
        int flag = 0;
        int weightMax;
        int cnt = 0;
        int k = 0;
        for(int i = 0; i < P; i++) {
            if(ranks[order[i]] == 0) {
                flag = 1;
                if(cnt % G == 0) {
                    weightMax = weight[order[i]];
                    k = order[i];
                }
                else {
                    if(weightMax < weight[order[i]]) {
                        weightMax = weight[order[i]];
                        ranks[k] = ran;
                        k = order[i];
                    }
                    else {
                        ranks[order[i]] = ran;
                    }
                    c++;
                }
                cnt++;
            }
        }
        if(ran == 2) {
            for(int i = 0; i < P; i++) {
                if(ranks[order[i]] == 0) {
                    ranks[order[i]] = 1;
                    break;
                }
            }
        }
        if(flag == 0) break;
    }
    for(int i = 0; i < P; i++) {
        if(i != 0) printf(" ");
        printf("%d", ranks[i]);
    }
    return 0;
}

 

posted @ 2018-02-23 09:55  Tobu  阅读(181)  评论(0编辑  收藏  举报