sicily_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五個(gè)隊(duì)伍,報(bào)紙編輯得到了一些人的投票馍乙,求出在這五隊(duì)伍的全排列中,和投票結(jié)果距離最小的排列吊输。
距離定義如下: 候選排列中和某一個(gè)投票的ranking不同的隊(duì)伍的對(duì)的個(gè)數(shù)铁追。
如候選排列ABCDE 和投票ABDCE 的距離為1.

思路

  1. 采用<algorithm>里面的next_permutation函數(shù)獲取全排列;
  2. 距離可以通過(guò)五個(gè)組位置之間的對(duì)比來(lái)確定琅束。

代碼

// Copyright (c) 2015 HuangJunjie@SYSU(SNO:13331087). All Rights Reserved.
// sicily 1006: http://soj.sysu.edu.cn/1006
#include <iostream>
#include <string>
#include <algorithm>

using namespace std;

// traverse all posible permutation and get the median ranking.
// @Param ranking: the array storing the votes.
// @Param num: the number of the votes.
// @return: a median ranking and its distance to votes.
pair<string, int> getMedian(string ranking[100 + 1], int num);

// a distance is defined as the number of different pairs' order of teams
// between string A and B.
// @Param A, B: the string to compare
int getDistance(string A, string B);

int main() {
  int n;                    // number of strings.
  string ranking[100 + 1];  // the votes from expert
  while (cin >> n && n) {
    // input a testcase of votes
    for (int i = 0; i < n; i++) cin >> ranking[i];

    // get the medien ranking
    pair<string, int> median = getMedian(ranking, n);

    // output a result.
    cout << median.first << " is the median ranking with value " << median.second << "." << endl;
  }
  return 0;
}

pair<string, int> getMedian(string ranking[100 + 1], int num) {
  // since the max distance is 10 and the number of string is no more than
  // 100, 1000 as the max distance is enough.
  int result = 1000 + 5, sum;
  string str = "ABCDE", median;
  do {
    sum = 0;
    for (int i = 0; i < num; i++) sum += getDistance(str, ranking[i]);
    if (sum < result) {
      result = sum;
      median = str;
    }
  } while (next_permutation(str.begin(), str.end()));

  return pair<string, int>(median, result);
}

int getDistance(string A, string B) {
  int pos[5], distance = 0;
  for (int i = 0; i < A.size(); i++) pos[A[i]-'A'] = i;
  for (int i = 0; i < B.size(); i++) {
    for (int j = i + 1; j < B.size(); j++) {
      if (pos[B[i] - 'A'] > pos[B[j] - 'A']) distance++;
    }
  }

  return distance;
}

參考

http://blog.csdn.net/jcjc918/article/details/9897703
http://blog.csdn.net/dijason/article/details/8147159

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末料滥,一起剝皮案震驚了整個(gè)濱河市艾船,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌屿岂,老刑警劉巖,帶你破解...
    沈念sama閱讀 221,820評(píng)論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件阻肩,死亡現(xiàn)場(chǎng)離奇詭異运授,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)吁朦,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,648評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門逗宜,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)剥啤,“玉大人,你說(shuō)我怎么就攤上這事府怯》揽” “怎么了?”我有些...
    開(kāi)封第一講書(shū)人閱讀 168,324評(píng)論 0 360
  • 文/不壞的土叔 我叫張陵冲簿,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我峦剔,道長(zhǎng)角钩,這世上最難降的妖魔是什么? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 59,714評(píng)論 1 297
  • 正文 為了忘掉前任惨险,我火速辦了婚禮脊髓,結(jié)果婚禮上辫愉,老公的妹妹穿的比我還像新娘将硝。我一直安慰自己,他們只是感情好依疼,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,724評(píng)論 6 397
  • 文/花漫 我一把揭開(kāi)白布涛贯。 她就那樣靜靜地躺著蔚出,像睡著了一般。 火紅的嫁衣襯著肌膚如雪骄酗。 梳的紋絲不亂的頭發(fā)上,一...
    開(kāi)封第一講書(shū)人閱讀 52,328評(píng)論 1 310
  • 那天睛琳,我揣著相機(jī)與錄音,去河邊找鬼师骗。 笑死,一個(gè)胖子當(dāng)著我的面吹牛辟癌,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播寡夹,決...
    沈念sama閱讀 40,897評(píng)論 3 421
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼厂置,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了昵济?” 一聲冷哼從身側(cè)響起,我...
    開(kāi)封第一講書(shū)人閱讀 39,804評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤传于,失蹤者是張志新(化名)和其女友劉穎醉顽,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體游添,經(jīng)...
    沈念sama閱讀 46,345評(píng)論 1 318
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,431評(píng)論 3 340
  • 正文 我和宋清朗相戀三年找都,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了廊酣。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,561評(píng)論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡晓猛,死狀恐怖凡辱,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情透乾,我是刑警寧澤磕秤,帶...
    沈念sama閱讀 36,238評(píng)論 5 350
  • 正文 年R本政府宣布捧韵,位于F島的核電站,受9級(jí)特大地震影響再来,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜其弊,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,928評(píng)論 3 334
  • 文/蒙蒙 一梭伐、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧糊识,春花似錦、人聲如沸赂苗。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 32,417評(píng)論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)败砂。三九已至赌渣,卻和暖如春昌犹,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背斜姥。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,528評(píng)論 1 272
  • 我被黑心中介騙來(lái)泰國(guó)打工铸敏, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留缚忧,地道東北人搞坝。 一個(gè)月前我還...
    沈念sama閱讀 48,983評(píng)論 3 376
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像敦第,于是被迫代替她去往敵國(guó)和親店量。 傳聞我的和親對(duì)象是個(gè)殘疾皇子芜果,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,573評(píng)論 2 359

推薦閱讀更多精彩內(nèi)容

  • 不必糾結(jié)前行的路是否有終點(diǎn)舀射, 因?yàn)槁愤叺娘L(fēng)景怀伦, 才是終點(diǎn)的意義脆烟; 不必徘徊未來(lái)還要走多遠(yuǎn)房待, 因?yàn)椋氵^(guò)的每一個(gè)現(xiàn)在...
    子喻女士閱讀 227評(píng)論 0 3
  • 線程池的種類 之前說(shuō)過(guò)ThreadPoolExecutor的用法拜鹤,jdkExecutors包下還封裝了一些其他類型...
    姜小碼閱讀 4,544評(píng)論 4 5
  • 如果是在三年前流椒,我一定會(huì)因?yàn)檫@本書(shū)的書(shū)名就放棄閱讀這本書(shū)。 書(shū)名多庸俗啊宣虾,一看就是打著巴菲特的幌子寫(xiě)的一本不痛不癢...
    SHEROtomorrow閱讀 296評(píng)論 0 0
  • 這世界說(shuō)大不大域那,說(shuō)小很小咙边。有些人次员,轉(zhuǎn)身,便是一輩子的漸行漸遠(yuǎn)淑蔚;有些人,轉(zhuǎn)角刹衫,還能有回眸的溫柔搞挣。這世上是否有緣分音羞,不...
    走在雨的縫中閱讀 298評(píng)論 2 5