[sicily online]1006. Team Rankings

来源:互联网 发布:办公软件手机版 编辑:程序博客网 时间:2024/05/29 11:12

Constraints

Time Limit: 1 secs, Memory Limit: 32 MB

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

4ABDCEBACDEABCEDACBDE0

题目分析:

刚开始错误的分析:刚看到这个题目时,考虑两个元素的相对位置关系,比如AB,查找每一个串中AB相对关系,A在B前面多,结果中B在A的后面,相反结果中B在A的前面,然后按照快排的思想来做,测了一天都没问题,但就是WA。最后才发现这种情况A,B,C,D,E没有传递性,比如A<B,B<C 不能判断A<C。所以这种方法pass了

网上有一种方法是把所有排列列出来,然后找出最小值(笔者怎么觉得这就是穷举啊),不过最后AC了,也就没多想。其中求出所有排列STL已经给出next_permutation函数了,参数应该是ABCDE开始的。

#include<iostream>#include<stdio.h>#include<cmath>#include<iomanip>#include <map>#include <vector>#include <string>#include <algorithm>#include <sstream>#include <stack>using namespace std;vector< map<char,int> >data;int n;//统计int cmpInt(char x1,char x2){int sum=0;for(int i=0;i<n;i++){if(data[i][x1]>data[i][x2])sum++;}return sum;}int main(){while(cin>>n&&n!=0){data.clear();data.resize(n);for(int i=0;i<n;i++){string tmp;cin>>tmp;for(int j=0;j<tmp.size();j++)data[i].insert(make_pair(tmp[j],j));}//end forvector<char> result;for(int i=0;i<5;i++)result.push_back('A'+i);int min=0;for(int i=0;i<5;i++){for(int j=i+1;j<5;j++)min+=cmpInt(result[i],result[j]);}vector<char> tmp=result;while(next_permutation(result.begin(),result.end())){int sum=0;for(int i=0;i<5;i++){for(int j=i+1;j<5;j++)sum+=cmpInt(result[i],result[j]);}if(sum<min){min=sum;tmp=result;}}for(int i=0;i<5;i++)cout<<tmp[i];cout<<" is the median ranking with value "<<min<<".";cout<<endl;}//end while }


原创粉丝点击