zoukankan      html  css  js  c++  java
  • PAT A1056 Mice and Rice (25分)

    #include<cstdio>
    #include<queue>
    #include<vector>
    using namespace std;
    const int N = 1010;
    queue<int> q;
    int w[N];
    int result[N];
    
    int main(){
        int np,ng;
        scanf("%d%d",&np,&ng);
        int weight;
        int candidate;
        for(int i = 0;i<np;i++){
            scanf("%d",&weight);
            w[i]=weight;
        }
        for(int i = 0;i<np;i++){
            scanf("%d",&candidate);
            q.push(candidate);
        }
        //contest
        int tnp = np;
        while(tnp!=1){//最后一场比赛就1人直接胜出
            int time = tnp/ng;
            int maxid;
            if(tnp%ng!=0) time++;
            for(int i = 0;i< time;i++ ){
                if(q.empty()==false){
                    if(tnp%ng==0||i!=time-1){//可以整除+不能整除且i不等于最后一次contest
                        maxid = q.front();
                        q.pop();
                        for(int j = 1;j<ng;j++){
                            int temp = q.front();
                            q.pop();
                            if(w[temp] > w[maxid]){ 
                                result[maxid] = time+1;//失败者直接计分
                                maxid = temp;
    
                            }else{
                                result[temp] = time+1;//失败者直接计分
                                
                            }
                        }
                        q.push(maxid);
                    }else{//不能整除且i等于最后一次contest
                        maxid = q.front();
                        q.pop();
                        for(int j = 1;j<tnp%ng;j++){
                            int temp = q.front();
                            q.pop();
                            if(w[temp] > w[maxid]){ 
                                result[maxid] = time+1;//失败者直接计分
                                maxid = temp;
                                
                            }else{
                                result[temp] = time+1;//失败者直接计分
                            }
                        }
                        q.push(maxid);
                    }                
                }
    
            }
            tnp = time;//下轮比赛人数
        }
        //胜者记为1
        result[q.front()] = 1;
        //print
        for(int i = 0;i<np;i++){
            printf("%d",result[i]);
            if(i!=np-1) printf(" ");
        }
        return 0;
    }
    
  • 相关阅读:
    linux安装nginx
    git配置多个SSH密钥
    webpack加载器安装node-sass失败的解决方法
    Vue-cli webpack打包之后index.html缺少引号的问题
    算法:一个数组中所有元素的最小公倍数
    JS 实现一个睡眠函数sleep
    剑指offer 牛客67道题集合
    剑指offer
    剑指offer
    剑指offer
  • 原文地址:https://www.cnblogs.com/shuibeng/p/13593836.html
Copyright © 2011-2022 走看看