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, 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.
#include <iostream>
#include <map>
#include <vector>
using namespace std;

//计算对应单个rankings的一个medianrank的distance
int rankdistance(string rank, map<char, int> order) {
	int sub_value = 0;

	for (int i = 0; i < rank.size(); i++) {
		for (int j = i+1; j < rank.size(); j++) {
			if (order[rank[i]] > order[rank[j]])//队排名越前,order[rank[i]]的值越小,此处为若单个rankings中的某个队排名比medianrank的高,则产生distanc
			  	sub_value++;
		}
	}

	return sub_value;
}
//计算对应一个rankings数组的一个medianrank的value
int rankvalue(vector<string> rankings, string rank) {
	int value = 0;

	for (int i = 0; i < rankings.size(); i++) {//对每个rankings数组的rank对medianrank计算其sub_value,累加成对所以rankings的value
		map<char, int> order; //此步很关键,使用map来记录rankings数组中的每个rank中的每个队的排名,以便计算distance
		for (int j = 0; j < rank.size(); j++) {
			order[rankings[i].at(j)] = j;
		}
		value += rankdistance(rank, order);
	}

	return value;
}
//枚举所有可能的medianrank并计算其对应一个rankings数组的value,同时选择value值最小且字母顺序最小的medianrank
void perminate(vector<string> rankings, string source, int &minvalue, string &result, int index) {//记得minvalue和result要设为引用类型,才能使43,44行起作用
	if (index == source.length()-1) {
		string temp;
		for (int i = 0; i < source.length(); i++) {
			temp += source[i];
		}
		int tempvalue = rankvalue(rankings, temp);
		//对所有可能的medinarank的value进行比较,选择value最小的medianrank ,此处使用<而非<=保证了相同的value的medianrank选择字母顺序最小的
		if (tempvalue < minvalue) {
			minvalue = tempvalue;
			result = temp;
		}

	} else {
		//由于程序执行时输入为ABCDE作为source,所以以下实现的该ABCDE五个字母的全排列为升序的
		for (int i = index; i < source.length(); i++) {
			swap(source[index], source[i]);
			perminate(rankings, source, minvalue, result, index+1);
			swap(source[index], source[i]);
		}
	}
}
int main() {
	int cases;
	while (cin >> cases && cases) {
		string str;
		vector<string> rankings;
		for (int i = 0; i < cases; i++) {
			cin >> str;
			rankings.push_back(str);
		}
		string source = "ABCDE";
		int minvalue = 1000;//由于5个字母的最大distance为10,所以minvalue的初始值设为很大
		string result;
		perminate(rankings, source, minvalue, result, 0);
		cout << result << " is the median ranking with value " << minvalue <<"." <<endl;
	}
	return 0;
}

  

时间: 2024-08-03 07:29:31

sicily 1006 team rankings 枚举解题的相关文章

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 edi

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

Sicily 1308. Dependencies among J 解题报告

题目:1308. Dependencies among J 思路: 比较简单的一道题,要知道m最早完成的时间,只需要找出所有需要在m之前完成的工作,将它们的完成时间加起来即可.这里使用vector的数组存储每个结点的邻接点,从结点m开始,依次宽度优先搜索m的每个邻接点...数组visited记录每个结点是否被访问过,遇到已经访问过的结点直接跳过. 读取的时候一开始没有找到解决办法,要读取一行的数字,并且要判断是否换行,可以首先读取第一个数即完成时间,然后用getchar读取一个字符,如果是'\n

2016 UESTC ACM Summer Training Team Selection (2)解题报告

总体来说,个人英语水平还太蹩脚,此次大部分时间都花在理解题意上了,而且到最后有些题目的意思还是不理解,orz... 链接→2016 UESTC ACM Summer Training Team Selection (2)  Problem A Popular Vote Accept: 0    Submit: 0 Time Limit: 2000 mSec  Problem Description In an election with more than two candidates, it

sicily 1150 简单魔方 队列解题

1150. 简单魔板 Constraints Time Limit: 1 secs, Memory Limit: 32 MB , Special Judge Description 魔板由8个大小相同方块组成,分别用涂上不同颜色,用1到8的数字表示. 其初始状态是 1 2 3 4 8 7 6 5 对魔板可进行三种基本操作: A操作(上下行互换): 8 7 6 5 1 2 3 4 B操作(每次以行循环右移一个): 4 1 2 3 5 8 7 6 C操作(中间四小块顺时针转一格): 1 7 2 4

sicily 1176 two ends 动态规划解题

1176. Two Ends Constraints Time Limit: 1 secs, Memory Limit: 64 MB Description In the two-player game "Two Ends", an even number of cards is laid out in a row. On each card, face up, is written a positive integer. Players take turns removing a c

sicily 1046 Plane Spotting 快速排序解题

1046. Plane Spotting Constraints Time Limit: 1 secs, Memory Limit: 32 MB Description Craig is fond of planes. Making photographs of planes forms a major part of his daily life. Since he tries to stimulate his social life, and since it’s quite a drive

uva 725 Division(暴力枚举) 解题心得

原题: Description Write a program that finds and displays all pairs of 5-digit numbers that between them use the digits 0 through 9once each, such that the first number divided by the second is equal to an integer N, where . That is, abcde / fghij =N w

【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