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 ...
随机推荐
- 【bzoj2653】middle 可持久化线段树区间合并
题目描述 一个长度为n的序列a,设其排过序之后为b,其中位数定义为b[n/2],其中a,b从0开始标号,除法取下整.给你一个长度为n的序列s.回答Q个这样的询问:s的左端点在[a,b]之间,右端点在[ ...
- Python os模块常用函数详解
当前使用平台: os.name #返回当前使用平台的代表字符,Windows用'nt'表示,Linux用'posix'表示 当前路径和文件 os.getcwd() #返回当前工作目录 os.listd ...
- CentOS 配置无线网络,开启wifi
背景:一台老笔记本安装CentOS7.x,最小安装模式,安装后无法开启wifi 1.先用NetworkManager包的nmcli命令检查网卡,发现无线网卡wlo1信息里有个错误plugin miss ...
- [CF1037H] Security
题目链接 codeforces. 洛谷. Solution 按照套路,可以\(SAM\)上线段树合并求出\(endpos\)集合,然后随便贪心一下就好了. #include<bits/stdc+ ...
- BZOJ2823 [AHOI2012]信号塔 【最小圆覆盖】
题目链接 BZOJ2823 题解 最小圆覆盖模板 都懒得再写一次 #include<iostream> #include<cstdio> #include<cmath&g ...
- POJ. 2253 Frogger (Dijkstra )
POJ. 2253 Frogger (Dijkstra ) 题意分析 首先给出n个点的坐标,其中第一个点的坐标为青蛙1的坐标,第二个点的坐标为青蛙2的坐标.给出的n个点,两两双向互通,求出由1到2可行 ...
- HDU.1233 还是畅通工程(Prim)
HDU.1233 还是畅通工程(Prim) 题意分析 首先给出n,代表村庄的个数 然后出n*(n-1)/2个信息,每个信息包括村庄的起点,终点,距离, 要求求出最小生成树的权值之和. 注意村庄的编号从 ...
- NOIP2017金秋冲刺训练营杯联赛模拟大奖赛第二轮Day2题解
肝了两题... T1一眼题,分解质因数,找出2的个数和5的个数取min输出 #include<iostream> #include<cstring> #include<c ...
- LibreOJ #6221. 幂数 !(数论+dfs+剪枝)
写新题然后艹翻标程的感觉真是舒爽啊... 这题就是个dfs...先预处理出sqrt(n)范围内的素数,然后dfs构造合法的数就行了. 直接暴搜会TLE,需要剪一剪枝,不需要跑到最后一层再计算答案,边构 ...
- Android Studio中进行单元测试
写单元测试类 1.创建单元测试文件夹,即新建一个用于单元测试的包,存放单元测试的类. 2.创建一个类如 ExampleTest,注意要继承自InstrumentationTestCase类. 3.创建 ...