1006. Team Rankings

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

4
ABDCE
BACDE
ABCED
ACBDE
0

Sample Output

ABCDE is the median ranking with value 4.

主要是注意输出的后面还有一个点。。

#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <map>

using namespace std;

int cmpInt(map<char, int> &rank, const string &str) {
    map<char, int> tmpRank;
    int sum = 0;
    for (int i = 0; i != 5; ++i) {
        tmpRank.insert(pair<char, int>(str[i], i));
    }
    for (int i = 0; i != 5; ++i) {
        for (int j = i + 1; j < 5; ++j) {
            int a = tmpRank[‘A‘ + i] - tmpRank[‘A‘ + j];
            int b = rank[‘A‘ + i] - rank[‘A‘ + j];
            if (a * b < 0) {
                ++sum;
            }
        }
    }
    return sum;
}

int main(int argc, char *argv[])
{
    int T;
    string str("ABCDE");
    string temp;
    string result;
    while (cin >> T && T != 0) {
        vector<map<char, int> > data;
        for (int i = 0; i != T; ++i) {
            cin >> temp;
            map<char, int> tmpRank;
            for (int i = 0; i != 5; ++i) {
                tmpRank.insert(pair<char, int>(temp[i], i));
            }
            data.push_back(tmpRank);
        }
        int minR = 0;
        bool f = true;
        do {
            int sum = 0;
            for (vector<map<char, int> >::iterator iter = data.begin();
                iter != data.end(); ++iter) {
                sum += cmpInt(*iter, str);
            }
            if ((minR == 0 && f) || minR > sum){
                    f = false;
                    minR = sum;
                    result = str;
            }
        } while (next_permutation(str.begin(), str.end()));
        cout << result << " is the median ranking with value " << minR << "."<< endl;
    }
}
时间: 2024-07-29 15:59:19

1006. Team Rankings的相关文章

sicily 1006 team rankings 枚举解题

1006. Team Rankings 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,

poj 2038 Team Rankings 枚举排列

//poj 2038 //sep9 #include <iostream> #include <algorithm> using namespace std; char s[128][8]; int count(char s1[],char s2[]) { int cnt=0; for(int i=0;i<5;++i) for(int j=i+1;j<5;++j){ int k; for(k=0;k<5;++k) if(s1[i]==s2[k]) break; f

【HDOJ】1310 Team Rankings

STL的应用,基本就是模拟题. 1 /* 1410 */ 2 #include <iostream> 3 #include <string> 4 #include <algorithm> 5 #include <map> 6 #include <cstdio> 7 #include <cstring> 8 using namespace std; 9 10 #define ONLINE_JUDGE 11 #define MAXN 12

编程题目分类(剪辑)

1. 编程入门 2. 数据结构 3. 字符串 4. 排序 5. 图遍历 6. 图算法 7. 搜索:剪枝,启发式搜索 8. 动态规划/递推 9. 分治/递归 10. 贪心 11. 模拟 12. 算术与代数 13. 组合问题 14. 数论 15. 网格,几何,计算几何 [编程入门] PC 110101, uva 100, The 3n+1 problem, 难度 1 PC 110102, uva 10189, Minesweeper, 难度 1 PC 110103, uva 10137, The T

HOJ 题目分类

转自:http://blog.sina.com.cn/s/blog_65f3869301011a1o.html ******************************************************************************* 简单题(包括枚举,二分查找,(复杂)模拟,基础数据结构(栈.队列),杂题等 ****************************************************************************

PAT 1006 换个格式输出 C语言

让我们用字母B来表示"百".字母S表示"十",用"12...n"来表示个位数字n(<10),换个格式来输出任一个不超过3位的正整数.例如234应该被输出为BBSSS1234,因为它有2个"百".3个"十".以及个位的4. 输入格式:每个测试输入包含1个测试用例,给出正整数n(<1000). 输出格式:每个测试用例的输出占一行,用规定的格式输出n. 输入样例1: 234 输出样例1: BBSSS1

UVa 1627 - Team them up!——[0-1背包]

Your task is to divide a number of persons into two teams, in such a way, that: everyone belongs to one of the teams; every team has at least one member; every person in the team knows every other person in his team; teams are as close in their sizes

TypeError: Error #1006: value 不是函数。

1.错误原因 TypeError: Error #1006: value 不是函数. at BasicChart/dataFunc()[E:\Flash Builder\Map\src\BasicChart.mxml:68] at mx.charts.chartClasses::Series/cacheDefaultValues()[E:\dev\4.0.0\frameworks\projects\datavisualization\src\mx\charts\chartClasses\Seri

HDU 3296 &amp; POJ 3138 Acm Team Section(数学)

题目链接: HDU: http://acm.hdu.edu.cn/showproblem.php?pid=3296 POJ:  http://poj.org/problem?id=3138 Acm Team Section Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 159    Accepted Submission(s): 47