您的位置:首页 > 其它

sicily 1006. Team Rankings

2015-10-04 17:50 357 查看

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, 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所有组合情况算出来,并保存在arr数组中。 
 * 然后计算每一种情况需要的value,保存在对应的value数组中,每种排列和它对应的value数组下标一致。 
 * 最后找到最小value的下标,对应位置的arr元素就是结果排列。 
 * 需要注意value相同情况。 
 */
/* 计算value的方法: 
 * 每对排列产生的value其实就是插入排序时的交换次数。 
 * 举个例子: 有ACEDB, XXXXX, XXXXX 3组需要比较的排列。 
 * 现在计算BADCE相对于ACEDB的value。 
 * 我们把ACEDB当作一种由小到大的排序顺序,也即 A=1,C=2,E=3,D=4,B=5。 
 * 现在我们找BADCE中的different order pair其实就是把这组排序。 
 * 拿这组做例子: 
 * BADCE->51423。 可见,只要是左边的数比右边大的pair都是不对应的pair,需要计入value。 
 * 我们做插入排序时其实就是找到所有顺序不对的pair,交换值。所以,交换值的次数其实就是我们找到的所有不对应的pair个数。 
 * 我们拿插入排序解一下value值: 
 * <1> 5 
 * <2> 5 1 -> 1 5 交换一次 value + 1 
 * <3> 1 5 4  -> 1 4 5 交换一次 
 * <4> 1 4 5 2  -> 1 2 4 5 交换2次 
 * <5> 1 2 4 5 3 -> 1 2 3 4 5 交换2次 
 * 1 + 1 + 2 + 2 = 6. * 我们根据肉眼观察,这组的value确实是6。
 * 可得答案。 
 */

#include
#include
#include
#define MAX 10000000
using namespace std;

string arr[131];
string cmp[101];
int value[121];
int num = 0;
void get_order(string str, int be, int end) {
	if (be > end) {
		arr[num++] = str;
	}
	else {
		for (int j = be; j <= end; j++) {
			swap(str[be], str[j]);
			get_order(str, be + 1, end);
			swap(str[j], str[be]);
		}
	}
}

int get_value(string ori, string cp) {
	map m;
	int sp[5];
	int count_swap = 0;
	for (int i = 0; i < 5; i++) {
		m.insert(make_pair(cp[i],i));
	}
	for (int i = 0; i < 5; i++) {
		sp[i] = m.find(ori[i])->second;
	}
	for (int i = 1; i < 5; i++) {
		for (int j = i; j >= 1; j--) {
			if (sp[j] < sp[j - 1]) {
				swap(sp[j], sp[j - 1]);
				count_swap++;
			}
			else {
				break;
			}
		}
	}
	return count_swap;
}

int main() {
	int n;
	cin >> n;
	string order = "ABCDE";
	get_order(order, 0, 4);

	while (n != 0) {
		for (int i = 0; i < n; i++)
			cin >> cmp[i];
		for (int i = 0; i < num; i++) {
			int c = 0;
			for (int j = 0; j < n; j++) {
				c += get_value(arr[i], cmp[j]);
			}
			value[i] = c;
		}
		int max = MAX;
		int flag;
		for (int i = 0; i < num; i++) {
			if (value[i] < max) {
				max = value[i];
				flag = i;
			}
			else if (value[i] == max) {
				if (arr[i] < arr[flag]) {
					flag = i;
				}
			}
		}
		cout << arr[flag] << " is the median ranking with value " << value[flag] << "." << endl;
		cin >> n;
	}
	return 0;
}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: