PAT A1056

这题看着咋就这么费解呢,,,

而且没有样例解释啊呜呜呜

原题面
就是说啊,有一堆老鼠,要按照分组比较谁沉。每组NG只,然后一共就是NP只,如果最后一组不够就不够去吧,还是一样比。每组的话最沉的那个晋级,剩下的名次一样。再把晋级的老鼠一起比,还是一样的比法。(有没有感觉像是递归)

刚刚说了,有点像是递归。

but,wait。其实dfs和bfs也会给我们一些启发。能递归还可以用queue来解决。

main idea

You can calculate the number of group and the rank of this turn for the mice don’t win.Maintain a queue and keep adding promoted mice to the queue.

每次你可以计算出组数以及没有晋级的老鼠的rank,将晋级的老鼠放入队列末端即可。

注意如果最后一组没有满数,那应该先用-1凑齐数量再操作。

attention

在queue的那个while中涉及一个变量叫qs,这个一开始赋值成了queue的大小。但是你要注意在往queue中疯狂添加-1的时候它也要变化呀!

#include<iostream>
#include<vector>
#include <queue>
#include <algorithm>
#include<cstdio>
#include <map>
#include <stack>
#include <math.h>
using namespace std;
int mice_num;
int each_group;
struct Mouse{
    int id;
    int weight;
    int rk;
}mice[10010];
//map<int,int> mmp;
void weight_rk(int weight,int rk){//can be optimizd
    if(weight!=-1) {
        for (int i = 0; i < mice_num; ++i) {
            if (mice[i].weight == weight) {
                mice[i].rk = rk;
                break;
            }

        }
    }
}

queue<int> q;

int main() {
    cin >> mice_num >> each_group;
    for (int i = 0; i < mice_num; ++i) {
        int a;
        cin >> a;
        mice[i].id = i;
        mice[i].weight = a;
    }
    for (int i = 0; i < mice_num; ++i) {
        int a;
        cin >> a;
        q.push(mice[a].weight);

    }
    while (!q.empty()) {//attention:the last rk of 1 & 2
        //the number of groups
        int qs = q.size();
        if (qs != 1) {
            //cout<<"qs/eacn"<<ceil(1.0*qs/each_group)<<endl;
            int group_number = ceil(1.0*qs / each_group);
            //cout<<"group_number:"<<group_number<<endl;
            //fill the queue
            while (qs < group_number * each_group) {
                q.push(-1);
                qs++;

            }//qs is the original number and the size of the queue is changed

            //calc the rk of this turn
            int rank = group_number + 1;
            //cout<<"rank"<<rank<<endl;
            for (int i = 0; i < group_number; ++i) {
                int maxn = -100;
                int storing[10010];
                //cout<<"the "<<i<<" group"<<endl;
                for (int j = 0; j < each_group; ++j) {
                    int temp = q.front();
                    storing[j] = temp;
                    maxn = max(maxn, temp);
                    q.pop();
                    //cout<<temp<<" "<<endl;
                }
                for (int j = 0; j < each_group; ++j) {
                    if (storing[j] == maxn) {
                        q.push(maxn);
                        //cout<<"maxn"<<maxn<<endl;
                    } else {
                        if (storing[j] != -1) {
                            weight_rk(storing[j], rank);
                        }
                    }

                }

            }
        }
        else{//only one
            int t=q.front();
            q.pop();
            weight_rk(t,1);

        }


    }
    for (int i = 0; i < mice_num; ++i) {
        cout << mice[i].rk;
        if(i!=mice_num-1){
            cout<<" ";
        }

    }
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值