1006 teamRanking

来源:互联网 发布:科比生涯数据 编辑:程序博客网 时间:2024/06/08 04:16

题目大意就是给出一堆关于ABCDE的排列,然后找出排列的中数。这个中数是所有排列中与输入排列的距离最小的排列,每当一对队伍的先后顺序不同,则距离加1。

解法是:由于只是关于ABCDE的排列,那么候选的答案只有A55,也就是120组而已,所以可以直接枚举。

#include<iostream>#include<algorithm>#include<string>using namespace std;string candidate;int index[101][5];int cmp[10][2] = {0,1, 0,2, 0,3, 0,4, 1,2, 1,3, 1,4, 2,3, 2,4, 3,4};int cal(int casNum, string cur){int sum = 0;int curIndex[5];for(int i = 0; i < 5; ++i)curIndex[cur[i]-'A'] = i;for(int i = 0; i < casNum; ++i){for(int j = 0; j < 10; ++j){int team1 = cmp[j][0];int team2 = cmp[j][1];int dif1 = curIndex[team1] - curIndex[team2];int dif2 = index[i][team1] - index[i][team2];if(dif1 * dif2 < 0)sum++;}}return sum;}int main(){int cas;cin >> cas;while(cas != 0){for(int i = 0; i < cas; ++i){cin >> candidate;for(int j = 0; j < 5; ++j)index[i][candidate[j]-'A'] = j;}string str = "ABCDE";int median = 1000000;int temp;string result;do{//cout << str << endl;temp = cal(cas, str);if(temp < median){median = temp;result = str;}}while(next_permutation(str.begin(), str.begin()+5));cout << result << " is the median ranking with value " << median << "." <<endl;cin >> cas;}} 



原创粉丝点击