1. 程式人生 > >PAT甲級1056(queue的用法)

PAT甲級1056(queue的用法)

題目

1056 Mice and Rice(25 分)
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 N
​P
​​ programmers. Then every N
​G
​​ 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 N
​G
​​ 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: N
​P
​​ and N
​G
​​ (≤1000), the number of programmers and the maximum number of mice in a group, respectively. If there are less than N
​G
​​ 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 N
​P
​​ distinct non-negative numbers W
​i
​​ (i=0,⋯,N
​P
​​ −1) where each W
​i
​​ is the weight of the i-th mouse respectively. The third line gives the initial playing order which is a permutation of 0,⋯,N
​P
​​ −1 (assume that the programmers are numbered from 0 to N
​P
​​ −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

題解

一開始我連題目都不是很懂的。首先難點在理解排名這,為什麼很多個5,沒有4這種疑問。其實是被淘汰的排名並列了。每次晉級者的名次都是晉級的人數,直到晉級人數為1。

然後是怎樣一組組比較的呢?第二排是所有老鼠,你可以想象是0號到10號的重量。而第三排就是要揪出第6只,第0只,第8只這樣一組,然後第7,10,5一組這樣進行比較。剩下不足一組3個的就兩個一組。每次每組一隻晉級。最後一組只有兩隻,獲勝的就以逸待勞,一眼就看出來它是第2名啦。

#include<iostream>
#include<queue>
#include<vector>
#include<algorithm>
using namespace std;
//每一輪間,淘汰者們的名次等於倖存人數+1;
struct node{
  int weight,index,index0,rank;
};
bool cmp(node a,node b){

  return a.index0<b.index0;
}
int main(){
  int np,ng,temp;
  scanf("%d %d",&np,&ng);
  vector<int> w(np);
  vector<node> v(np);//比較過程中的節點
  for(int i=0;i<np;i++){
    scanf("%d",&w[i]);
  }
  for(int i=0;i<np;i++){
    scanf("%d",&temp);
    v[i].weight=w[temp];
    v[i].index=i;//,把序號為6的放在了第一位,比較過程用的下標
    v[i].index0=temp;//原來的下標,要輸出的開始下標
  }
  queue<node>q;
  for(int i=0;i<np;i++){
    q.push(v[i]);
  }
  while(!q.empty()){
    int size=q.size();
    //先考慮最後的極端情況,只剩一個
    if(size==1){
      node last=q.front();
      v[last.index].rank=1;
      break;
    }
    int group=size/ng;
    if(size%ng!=0){
      group+=1;
    }

    node maxnode;
    int maxn=-1,cnt=0;//maxn用於比較重量
    for(int i=0;i<size;i++){
    //這個for迴圈結果是:過完了第一波,剩下每組最大的那個在queue裡
      node last=q.front();
      v[last.index].rank=group+1;//排定名次後吐出該點
      q.pop();//先出來先進行比較,cnt+1
      cnt++;
      if(last.weight>maxn){
        maxn=last.weight;
        maxnode=last;
      }//先估摸好最大的
      if(cnt==ng||i==size-1){
        //湊夠一組或者到了最後
        cnt=0;//一組比完了
        maxn=-1;//進入新的一組對比
        q.push(maxnode);//與之前的maxnode比
      }
    }
  }
  sort(v.begin(),v.end(),cmp);

   for(int i=0;i<np;i++){

     if(i!=0) printf(" ");
     printf("%d",v[i].rank);
   }


  return 0;
} 

這裡的程式碼是從柳神那邊學來的,完全理解了並加上註釋才敢發出來。我於昨晚11點多,支付寶給她打了一塊錢的賞試試水,結果沒有上打賞名單額。

一開始是非完全正確的,因為自己寫得時候區域性變數cnt沒有初始化為0。一般全域性變數可不用,但區域性變數一定要初始化(可能這句不完全正確)。