[关闭]
@DingCao-HJJ 2015-10-11T15:55:32.000000Z 字数 3862 阅读 1034

sicily_1006 Team Rankings

sicily


题目

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

4
ABDCE
BACDE
ABCED
ACBDE
0

Sample Output

ABCDE is the median ranking with value 4.

题目大意

有ABCDE五个队伍,报纸编辑得到了一些人的投票,求出在这五队伍的全排列中,和投票结果距离最小的排列。
距离定义如下: 候选排列中和某一个投票的ranking不同的队伍的对的个数。
如候选排列ABCDE 和投票ABDCE 的距离为1.

思路

  1. 采用<algorithm>里面的next_permutation函数获取全排列;
  2. 距离可以通过五个组位置之间的对比来确定。

代码

  1. // Copyright (c) 2015 HuangJunjie@SYSU(SNO:13331087). All Rights Reserved.
  2. // sicily 1006: http://soj.sysu.edu.cn/1006
  3. #include <iostream>
  4. #include <string>
  5. #include <algorithm>
  6. using namespace std;
  7. // traverse all posible permutation and get the median ranking.
  8. // @Param ranking: the array storing the votes.
  9. // @Param num: the number of the votes.
  10. // @return: a median ranking and its distance to votes.
  11. pair<string, int> getMedian(string ranking[100 + 1], int num);
  12. // a distance is defined as the number of different pairs' order of teams
  13. // between string A and B.
  14. // @Param A, B: the string to compare
  15. int getDistance(string A, string B);
  16. int main() {
  17. int n; // number of strings.
  18. string ranking[100 + 1]; // the votes from expert
  19. while (cin >> n && n) {
  20. // input a testcase of votes
  21. for (int i = 0; i < n; i++) cin >> ranking[i];
  22. // get the medien ranking
  23. pair<string, int> median = getMedian(ranking, n);
  24. // output a result.
  25. cout << median.first << " is the median ranking with value " << median.second << "." << endl;
  26. }
  27. return 0;
  28. }
  29. pair<string, int> getMedian(string ranking[100 + 1], int num) {
  30. // since the max distance is 10 and the number of string is no more than
  31. // 100, 1000 as the max distance is enough.
  32. int result = 1000 + 5, sum;
  33. string str = "ABCDE", median;
  34. do {
  35. sum = 0;
  36. for (int i = 0; i < num; i++) sum += getDistance(str, ranking[i]);
  37. if (sum < result) {
  38. result = sum;
  39. median = str;
  40. }
  41. } while (next_permutation(str.begin(), str.end()));
  42. return pair<string, int>(median, result);
  43. }
  44. int getDistance(string A, string B) {
  45. int pos[5], distance = 0;
  46. for (int i = 0; i < A.size(); i++) pos[A[i]-'A'] = i;
  47. for (int i = 0; i < B.size(); i++) {
  48. for (int j = i + 1; j < B.size(); j++) {
  49. if (pos[B[i] - 'A'] > pos[B[j] - 'A']) distance++;
  50. }
  51. }
  52. return distance;
  53. }

参考

http://blog.csdn.net/jcjc918/article/details/9897703
http://blog.csdn.net/dijason/article/details/8147159

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注