1006. Team Rankings

来源:互联网 发布:seo教程 百度云 编辑:程序博客网 时间:2024/06/06 15:41
Description
It's preseason and the local newspaper wants to publish a preseason ranking of the teams in the local amateur basketball league. The teams are the Ants, the Buckets, the Cats, the Dribblers, and the Elephants. When Scoop McGee, sports editor of the paper, gets the rankings from the selected local experts down at the hardware store, he's dismayed to find that there doesn't appear to be total agreement and so he's wondering what ranking to publish that would most accurately reflect the rankings he got from the experts. He’s found that finding the median ranking from among all possible rankings is one way to go.

The median ranking is computed as follows: Given any two rankings, for instance ACDBE and ABCDE, the distance between the two rankings is defined as the total number of pairs of teams that are given different relative orderings. In our example, the pair B, C is given a different ordering by the two rankings. (The first ranking has C above B while the second ranking has the opposite.) The only other pair that the two rankings disagree on is B, D; thus, the distance between these two rankings is 2. The median ranking of a set of rankings is that ranking whose sum of distances to all the given rankings is minimal. (Note we could have more than one median ranking.) The median ranking may or may not be one of the given rankings.

Suppose there are 4 voters that have given the rankings: ABDCE, BACDE, ABCED and ACBDE. Consider two candidate median rankings ABCDE and CDEAB. The sum of distances from the ranking ABCDE to the four voted rankings is 1 + 1 + 1 + 1 = 4. We'll call this sum the value of the ranking ABCDE. The value of the ranking CDEAB is 7 + 7 + 7 + 5 = 26.

It turns out that ABCDE is in fact the median ranking with a value of 4.

Input
There will be multiple input sets. Input for each set is a positive integer n on a line by itself, followed by n lines (n no more than 100), each containing a permutation of the letters A, B, C, D and E, left-justified with no spaces. The final input set is followed by a line containing a 0, indicating end of input.
Output
Output for each input set should be one line of the form:

ranking is the median ranking with value value.

Of course ranking should be replaced by the correct ranking and value with the correct value. If there is more than one median ranking, you should output the one which comes first alphabetically.

Sample Input
Copy sample input to clipboard
4ABDCEBACDEABCEDACBDE0
Sample Output
ABCDE is the median ranking with value 4.
题目大意:
        给出一个长度为n的打印任务队列,每个任务有优先级。每次从队列头得到一个任务,如果它是剩余任务中优先级最高的,则打印它,否则放到队列尾。求出其中某个特定任务是第几个被执行的。
        n<=100
解题思路:
        使用队列直接模拟。
        取出队列头判断是否打印,如果打印则已打印任务数加一。
        直到特定的任务完成,输出答案。
*/  
#include <iostream> 
#include <deque> 
using namespace std; 
   
//重点1:学会灵活使用结构体,务必记住!  
struct node{ 
   int priority; 
   int position; 
}; 
/*重点2:STL容器:详细见笔记! 
deque、vector容器,相当于可变化数组,一般使用insert、erase插入删除,使用数组下标访问(也可以使用迭代器); 
② list容器一般使用insert、erase插入删除,使用迭代器iterator *it 访问; 
③ stack、queue一般使用push插入、使用pop删除,使用top、front访问,不能使用迭代器; 
*/  
// Problem#: 1006// Submission#: 4971474// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/// All Copyright reserved by Informatic Lab of Sun Yat-sen University//这里用到了一个全排列函数/*#include <iostream>#include <algorithm>#include <string> using namespace std; int main(){    string str;    cin >> str;    sort(str.begin(), str.end());    cout << str << endl;    while (next_permutation(str.begin(), str.end())) //该函数为bool类型函数    {        cout << str << endl;   // 输出每一个排列    }    return 0;}*/
以下为该题代码:#include <iostream>  #include <cstring>  #include <algorithm>  using namespace std;    int find(char x,char str[])                         //寻找字符x在指定字符串中的位置  {      int index;      for (int i = 0; i < 5; i++)      {          if (str[i] == x)          {              index = i;              return index;          }      }  }  int main()  {      char p[160][5], cor[106][5],temp[5],res[5];      char chr[5] = {'A','B','C','D','E'};      int n,i,j,k,r,t;      int num,cnt = 1;      for(i = 0;i<5;i++)          p[0][i] = chr[i];      while(next_permutation(chr,chr+5))             //首先得到所有的全排列,保存在p[160][5]中      {          for(int i =0;i<5;i++)              p[cnt][i] = chr[i];          cnt++;                                     //cnt为全排列数,cnt = 5! = 120      }      while(cin>>n,n)      {          for(i = 0;i<n;i++)              for(k = 0;k<5;k++)                  cin>>cor[i][k];          int min = 10000;          for(i = 0;i<cnt;i++)          {              for(k = 0;k<5;k++)                      //针对每一个全排列得到逆序数和                  temp[k] = p[i][k];              num = 0;                for(j = 0;j<n;j++)                      //这里是求逆序数和              {                  for(r = 0;r<4;r++)                      for(t = r+1;t<5;t++)                      {                          if(find(cor[j][r],temp)>find(cor[j][t],temp))                                num ++;                      }              }              if(min>num)                              //min保存所有全排列中最小的逆序数和              {                  min = num;                   memcpy(res,temp,sizeof(temp));              }          }      for(i = 0;i<5;i++)              //p数组的全排列已经按字典序排列,故res中保存的就是相同逆序数和下字典序最小的全排列          cout<<res[i];      cout<< " is the median ranking with value " << min << "." << endl;      }      return 0;  }                     
    


题目大意:
09.        给出一个长度为n的打印任务队列,每个任务有优先级。每次从队列头得到一个任务,如果它是剩余任务中优先级最高的,则打印它,否则放到队列尾。求出其中某个特定任务是第几个被执行的。
10.        n<=100
11.解题思路:
12.        使用队列直接模拟。
13.        取出队列头判断是否打印,如果打印则已打印任务数加一。
14.        直到特定的任务完成,输出答案。
15.*/  
16.#include <iostream> 
17.#include <deque> 
18.usingnamespace std; 
19.   
20.//重点1:学会灵活使用结构体,务必记住!  
21.structnode{ 
22.   intpriority; 
23.   intposition; 
24.}; 
25./*重点2:STL容器:详细见笔记! 
26.① deque、vector容器,相当于可变化数组,一般使用insert、erase插入删除,使用数组下标访问(也可以使用迭代器); 
27.② list容器一般使用insert、erase插入删除,使用迭代器iterator *it 访问; 
28.③ stack、queue一般使用push插入、使用pop删除,使用top、front访问,不能使用迭代器; 
29.*/  
30.intmain(){ 
31.       
32.    intt; 
33.    intm; 
34.    intn; 
35.    intsum = 0;    
36.    deque <node> jobs; 
37.    structnode job[101]; 
38.      
39.    cin >> t; 
40.    //错误3:循环错误for(int i = 0 ; i < t ; t++){  
41.    for(inti = 0 ; i < t ; i++){      
42.        jobs.clear();  //错误1:忘记清空  
43.        sum = 0;       //错误2:忘记置零  
44.        cin >> n >> m; 
45.        for(int j=0;j<n;j++){ 
46.            cin >> job[j].priority; 
47.            job[j].position = j; 
48.               
49.        }  
50.        for(intj=0;j<n;j++){ 
51.            jobs.insert(jobs.end(),job[j]); 
52.        
53.        //错误3:循环错误for(int t=0;t<n-1;t++){ 应该改为判断队列是否为空 for(int k=0;jobs.size()!=0;k++){ 
54.        for(intk=0;k<10000;k++){ 
55.            if(jobs.size()!=0){  //重点3:判断是否为空,否则会发生溢出  
56.               boolflag=false;                  
57.                                  //3.错误:for(int j=0;j<n;j++){ n需要变化,一般改为jobs.size()    
58.               for(intj=0;j<jobs.size();j++){ 
59.                  if(jobs[0].priority < jobs[j].priority){ 
60.                      flag =true
61.                      break
62.                  }            
63.               }                 
64.               if(flag ==true){ 
65.                   structnode temp = jobs[0]; 
66.                   jobs.erase(jobs.begin()); 
67.                   jobs.insert(jobs.end(),temp); 
68.               }else
69.                   structnode  temp = jobs[0]; 
70.                   jobs.erase(jobs.begin()); 
71.                   sum ++ ; 
72.                   //n = jobs.size(); 
73.                   if(temp.position == m)//错误4:if语句忘记{}  
74.                   
75.                       cout << sum <<endl; 
76.                       break
77.                   
78.               }            
79.        }                  
80.      
81.    
82.   //system("pause"); 
83.   return0; 
84.}
0 0