PAT A1056 Mice and Rice ——昨夜雨疏风骤

PAT A1056 Mice and Rice

  • 最开始没想到用队列,数组循环计数把自己绕死了,后来看了书上
  • 这里注意几点:1.push最大值是在循环结束之后;2.只有if(i * K + j == tmp) break;用到了i * K + j(实际上还不如直接用cnt ++),其他都是用队头出来的idx;3.max要给初值,或者说每次循环用过之后要复位成最小值,不然后来者可能盖不过
  • 有一个点挂了,先睡了
  • 经分析发现当只有一个数的时候不会进入循环,无法给rank赋值,那个测试点也就是这么回事,所以需要把ranky[order.front()] = 1;扔到循环外面即可
  • 在这里插入图片描述
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <math.h>
#include <set>
#include <map>
#include <unordered_map>
#include <stack>


using namespace std;


vector<int> weight,ranky;
queue<int> order;

#define DEBUG 


int main(){

#ifdef DEBUG
	freopen("1.txt","r",stdin);	
#endif

    int num,K;
    cin >> num >> K;
    for(int i = 0;i < num;i ++){
        int w;
        cin >> w;
        weight.push_back(w);
    }
    for(int i = 0;i < num;i ++){
        int od;
        cin >> od;
        order.push(od);
    }
    ranky.resize(num);
    int nowNG,tmp = num;
    while(order.size() > 1){
        if(tmp % K == 0) nowNG = tmp / K;
        else nowNG = tmp / K + 1;
        for(int i = 0;i < nowNG;i ++){
            int max = -1,maxi = -1;
            for(int j = 0;j < K;j ++){
                if(i * K + j == tmp) break;
                int idx = order.front();
                order.pop();
                if(weight[idx] > max){
                    max = weight[idx];
                    maxi = idx;
                }
                ranky[idx] = nowNG + 1;
            }
            order.push(maxi);
        }
        tmp = nowNG;
        //if(order.size() == 1) ranky[order.front()] = 1;
    }
    ranky[order.front()] = 1;
    for(int i = 0;i < num;i ++){
        cout << ranky[i];
        if(i < num - 1) cout << ' ';
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值