A1056Mice and Rice (25分)

Posted tsruixi

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了A1056Mice and Rice (25分)相关的知识,希望对你有一定的参考价值。

一、技术总结

  1. 这一题是使用了queue队列的知识,但是也是用了很多辅助知识。
  2. 最重要的一步是,不断的选择最大的体重,和进而排序。
  3. 第一步是创建结构体,里面包含体重weight,现在顺序index,原始顺序index0,rank排名
  4. 然后把存储好在vector里的存入queue队列中,然后只要没有到最后一个即不空的情况下,就一直分组排名

二、参考代码

#include<iostream>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std;
struct node{
    int weight, index0, index, rank;
};
bool cmp(node a, node b){
    return a.index0 < b.index0;
}

int main(){
    int n, g, num;
    scanf("%d%d", &n, &g);
    vector<int> v(n);
    vector<node> w(n);
    for(int i = 0; i < n; i++){
        scanf("%d", &v[i]);
    }
    for(int i = 0; i < n; i++){
        scanf("%d", &num);
        w[i].weight = v[num];
        w[i].index = i;
        w[i].index0 = num;
    }
    queue<node> q;
    for(int i = 0; i < n; i++){
        q.push(w[i]);
    }
    while(!q.empty()){
        int size = q.size();
        if(size == 1){
            node temp = q.front();
            w[temp.index].rank = 1;
            break;
        }
        int group = size / g;
        if(size % g != 0){
            group++;
        }
        node maxnode;
        int cnt = 0, max = -1;
        for(int i = 0; i < size; i++){
            node temp = q.front();
            w[temp.index].rank = group + 1;
            q.pop();
            cnt++;
            if(temp.weight > max){
                maxnode = temp;
                max = temp.weight;
            }
            if(cnt == g || i == size - 1){
                cnt = 0;
                max = -1;
                q.push(maxnode);
            }
        }
    }
    sort(w.begin(), w.end(), cmp);
    for(int i = 0; i < n; i++){
        if(i != 0) printf(" ");
        printf("%d", w[i].rank);
    }
    return 0;
}

以上是关于A1056Mice and Rice (25分)的主要内容,如果未能解决你的问题,请参考以下文章

1056 Mice and Rice (25 分)难度: 一般 / 知识点: 模拟

PAT甲级1056 Mice and Rice (25 分)

PAT 1056 Mice and Rice (25)

PAT甲级--Mice and Rice (25)

PAT (Advanced Level) 1056. Mice and Rice (25)

数据结构专题——队列的应用 A1056.Mice and Rice ( 25)