1006. Team Rankings
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.
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.
4
ABDCE
BACDE
ABCED
ACBDE
0
ABCDE is the median ranking with value 4.
主要是注意输出的后面还有一个点。。
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <map> using namespace std; int cmpInt(map<char, int> &rank, const string &str) {
map<char, int> tmpRank;
int sum = ;
for (int i = ; i != ; ++i) {
tmpRank.insert(pair<char, int>(str[i], i));
}
for (int i = ; i != ; ++i) {
for (int j = i + ; j < ; ++j) {
int a = tmpRank['A' + i] - tmpRank['A' + j];
int b = rank['A' + i] - rank['A' + j];
if (a * b < ) {
++sum;
}
}
}
return sum;
} int main(int argc, char *argv[])
{
int T;
string str("ABCDE");
string temp;
string result;
while (cin >> T && T != ) {
vector<map<char, int> > data;
for (int i = ; i != T; ++i) {
cin >> temp;
map<char, int> tmpRank;
for (int i = ; i != ; ++i) {
tmpRank.insert(pair<char, int>(temp[i], i));
}
data.push_back(tmpRank);
}
int minR = ;
bool f = true;
do {
int sum = ;
for (vector<map<char, int> >::iterator iter = data.begin();
iter != data.end(); ++iter) {
sum += cmpInt(*iter, str);
}
if ((minR == && f) || minR > sum){
f = false;
minR = sum;
result = str;
}
} while (next_permutation(str.begin(), str.end()));
cout << result << " is the median ranking with value " << minR << "."<< endl;
}
}
1006. Team Rankings的更多相关文章
- 【HDOJ】1310 Team Rankings
STL的应用,基本就是模拟题. /* 1410 */ #include <iostream> #include <string> #include <algorithm& ...
- poj 2038 Team Rankings 枚举排列
//poj 2038 //sep9 #include <iostream> #include <algorithm> using namespace std; char s[1 ...
- HOJ题目分类
各种杂题,水题,模拟,包括简单数论. 1001 A+B 1002 A+B+C 1009 Fat Cat 1010 The Angle 1011 Unix ls 1012 Decoding Task 1 ...
- [转载]John Burkardt搜集的FORTRAN源代码
Over the years, I have collected, modified, adapted, adopted or created a number of software package ...
- 2017 Multi-University Training Contest - Team 1 1006&&HDU 6038 Function【DFS+数论】
Function Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total ...
- HDU 6038.Function-数学+思维 (2017 Multi-University Training Contest - Team 1 1006)
学长讲座讲过的,代码也讲过了,然而,当时上课没来听,听代码的时候也一脸o((⊙﹏⊙))o 我的妈呀,语文不好是硬伤,看题意看了好久好久好久(死一死)... 数学+思维题,代码懂了,也能写出来,但是还是 ...
- HDU 6166.Senior Pan()-最短路(Dijkstra添加超源点、超汇点)+二进制划分集合 (2017 Multi-University Training Contest - Team 9 1006)
学长好久之前讲的,本来好久好久之前就要写题解的,一直都没写,懒死_(:з」∠)_ Senior Pan Time Limit: 12000/6000 MS (Java/Others) Memor ...
- 2017ACM暑期多校联合训练 - Team 8 1006 HDU 6138 Fleet of the Eternal Throne (字符串处理 AC自动机)
题目链接 Problem Description The Eternal Fleet was built many centuries ago before the time of Valkorion ...
- 2017ACM暑期多校联合训练 - Team 5 1006 HDU 5205 Rikka with Graph (找规律)
题目链接 Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situation, s ...
随机推荐
- HUAS 1480 虫洞(最短路)
把每个点拆成两个点,分别表示黑洞和白洞,然后按题意模拟加边跑最短路即可. # include <cstdio> # include <cstring> # include &l ...
- BZOJ 1227 虔诚的墓主人(离散化+树状数组)
题目中矩形的尺寸太大,导致墓地的数目太多,如果我们统计每一个墓地的虔诚度,超时是一定的. 而常青树的数目<=1e5.这启发我们从树的方向去思考. 考虑一行没有树的情况,显然这一行的墓地的虔诚度之 ...
- 【bzoj4580】[Usaco2016 Open]248 区间dp
题目描述 Bessie likes downloading games to play on her cell phone, even though she does find the small t ...
- vue-cli开发时,ajax跨域详细解决办法
在config/index.js中进行如下配置 [即在进行ajax请求时,地址中任何以/api开头的请求地址都被解析为目标地址,target就是你想要的后台接口地址] proxyTable: { '/ ...
- 算法语言Scheme修订6报告 R6RS简体中文翻译
算法语言Scheme修订6报告 R6RS简体中文翻译 来源 https://r6rs.mrliu.org/ MICHAEL SPERBERR. KENT DYBVIG, MATTHEW FLATT ...
- [Vue源码分析] v-model实现原理
最近小组有个关于vue源码分析的分享会,提前准备一下… 前言:我们都知道使用v-model可以实现数据的双向绑定,及实现数据的变化驱动dom的更新,dom的更新影响数据的变化.那么v-model是怎么 ...
- BZOJ5157 & 洛谷3970:[TJOI2014]上升子序列——题解
https://www.lydsy.com/JudgeOnline/problem.php?id=5157 https://www.luogu.org/problemnew/show/P3970 给定 ...
- 剑桥offer(41~50)
41.题目描述 求1+2+3+...+n,要求不能使用乘除法.for.while.if.else.switch.case等关键字及条件判断语句(A?B:C). class Solution { pub ...
- HDU 1044 BFS
Collect More Jewels Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- 名人问题/名流问题/Celebrity
问题描述:名人问题一个名人就是指这样一个人:所有其他人都认识他,并且他不认识任何其他人.现在有一个N个人的集合,以及他们之间的认识关系.求一个算法找出其中的名人(如果有的话)或者判断出没有名人(如果没 ...