ZOJ 2207 Team Rankings

来源:互联网 发布:sql server 2016 编辑:程序博客网 时间:2024/05/25 21:33

枚举ABCDE排列即可,next_permutation好用.

#include <iostream>#include <algorithm>#include <cstdio>#include <cstring>using namespace std;struct ranks{int v[6];char str[6];}rks[101];int n;int main(){while (scanf("%d", &n) && n){char ans[6];int minV = 10000 ;for (int i = 0; i < n; ++i){scanf("%s", rks[i].str);for (int j = 0; j < 5; ++j){rks[i].v[rks[i].str[j] - 'A'] = j;}}char per[] = "ABCDE";do {int v[6];for (int j = 0; j < 5; ++j){v[per[j] - 'A'] = j;}int sum = 0;for (int i = 0; i < n; ++i){int cnt = 0;for (char a = 'A'; a <= 'E'; ++a){for (int b = a + 1; b <= 'E'; ++b){if(rks[i].v[a - 'A'] < rks[i].v[b - 'A'] && v[a - 'A'] > v[b - 'A'] ||rks[i].v[a - 'A'] > rks[i].v[b - 'A'] && v[a - 'A'] < v[b - 'A']){cnt++;}}}sum += cnt;}if(sum < minV){minV = sum;memcpy(ans, per, sizeof( per));}} while (next_permutation(per, per + 5));printf("%s is the median ranking with value %d.\n",ans, minV);}return 0;}


原创粉丝点击