1006. team rankings

Source: Internet
Author: User
Descriptionit'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 wowould 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 cocould 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. inputthere 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. outputoutput for each input set shoshould be one line of the form:

Ranking is the median ranking with value.

Of course ranking shocould 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.

Note that there is another point behind the output ..

#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;    }}

 

1006. team rankings

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.