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 <algorithm>
#include <string>

using namespace std;
using std::string;
int main()
{
	int n;
	string str[200];
	string a = "ABCDE";
	string last = "ABCDE";
	while (cin >> n && n != 0)
	{
		int min = 1000;
		int temp = 0;
		char out[6];
		for (int i = 0; i < n; i++)
			cin >> str[i];
		do
		{
			temp = 0;
			for (int i = 0; i < n; i++)
				for (int j = 0; j < 4; j++)
					for (int k = j + 1; k < 5; k++)
					{
						int first_str = str[i].find(a[j]);
						int second_str = str[i].find(a[k]);
						
						if (first_str > second_str)
						{
							temp++;
						}
					}
			
			if (temp < min) {
				min = temp;
				last = a;
			}
		} while (next_permutation(a.begin(), a.end()));

		cout << last << " is the median ranking with value " << min << "." << endl;
	}
	
	system("pause");
	return 0;
}


The team Test just finished an exciting JB/ICPC competition. They are eager to know their ranking, but the board has been closed. So they can only guess their ranking by observing the submissions of other teams. Now let’s briefly introduce the rules of ranking. Each team has two statistics: the number of solved problems and penalty time. All teams are ranked according to their numbers of solved problems. If the numbers of solved problems of two different teams are equal, then they are ranked according to the penalty time. Each time a team passes a problem, the number of solved problems +1, and the time (in minutes) elapsed from the beginning of the competition is added to their penalty time. If a team has multiple submissions, each of the failed submissions will result in a 20 added to their penalty time. At the time when the competition lasts for four hours, the board will stop updating. In other words, the board is "frozen". After that, you can only see the submission status of other teams (the number of attempts, the time of the last submission), but you cannot see whether those submissions are accepted or not. In addition, the competition lasts for five hours in total. In particular, all teams will not submit solutions to the problem after getting an "Accepted". If two teams have the same number of solved problems and penalty time, then they are ranked the same. Please help the team Test guess the highest and lowest rankings they can reach based on the current board. The input contains several test cases, and the first line contains a positive integer indicating the number of test cases, . For each test case, the first line is a positive integer indicating the number of other teams, where . Then for each team, the first line are two integers and , where indicating the number of solved problems and indicating the penalty time of this team. The second line contains an integer indicating the submissions of problems of this team after the board is frozen. Then following lines, each line contains two positive integers and indicating the time of the last submission and the submit times of a problem of this team. Finally there is one line contains two integers and indicating the number of solved problems and the penalty time of the team Test. For each test case, output a line containing two positive integers indicating the highest and lowest rankings of the team Test. 输入样例 1 1 1 30 1 250 2 1 20 输出样例 1 2
12-19
The following is the current ranking rules for the ICPC Asia EC Online Qualifiers, and there will be two online contests.\n\nIn each contest, only the rank of the top-ranked team from each university will be taken as the score of that university;\nIn each contest, participating universities will be ranked according to their scores;\nThe two rankings of universities are combined using the merge sorting method. For any two universities that obtain the same ranking in different contests, the university that received this ranking in the first contest will be ranked first.\nDelete duplicate universities and obtain the final ranking of all participating universities (only the highest rankings for each university are retained).\nNow assuming that there are n teams in the first contest and m teams in the second contest.\nFor each contest, given the ranking of each team and the university to which it belongs, please output the final ranking of all participating universities according to the above rules.\nYou can better understand this process through the sample.\nInput\nThe first line contains two integers n,m (1≤n,m≤104) , representing the number of teams participating in the first contest and the second contest.\nThen following n lines, the i-th line contains a string si (1≤∣si∣≤10) only consisting of uppercase letters, representing the abbreviation of the university to which the i-th ranked team in the first contest belongs.\nThen following m lines, the i-th line contains a string ti (1≤∣ti∣≤10) only consisting of uppercase letters, representing the abbreviation of the university to which the i-th ranked team in the second contest belongs.\nIt’s guaranteed that each university has only one abbreviation.\nOutput\nOutput several lines, the i-th line contains a string, representing the abbreviation of the i-th ranked university in the final ranking.\nYou should ensure that the abbreviation of any participating universities appears exactly once. 翻译一下
08-17
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值