【HDOJ】1310 Team Rankings

STL的应用,基本就是模拟题。

 1 /* 1410 */
 2 #include <iostream>
 3 #include <string>
 4 #include <algorithm>
 5 #include <map>
 6 #include <cstdio>
 7 #include <cstring>
 8 using namespace std;
 9 
10 #define ONLINE_JUDGE
11 #define MAXN 125
12 #define INF 0xfffffff
13 
14 int nn;
15 int dis[MAXN][MAXN];
16 string buf[MAXN];
17 bool visit[5];
18 map<string, int> tb;
19 int a[MAXN];
20 
21 int myabs(int x) {
22     return x<0 ? -x:x;
23 }
24 
25 void init() {
26     string s = "ABCDE";
27     nn = 0;
28     int i, j, k, p;
29     int sum, tmp;
30     
31     do {
32         buf[nn] = s;
33         tb[s] = nn++;
34     } while (next_permutation(s.begin(), s.end()));
35     
36     for (i=0; i<nn; ++i) {
37         dis[i][i] = 0;
38         for (j=i+1; j<nn; ++j) {
39             sum = 0;
40             memset(visit, false, sizeof(visit));
41             for (k=0; k<5; ++k) {
42                 for (p=0; p<5; ++p) {
43                     if (buf[j][p] == buf[i][k])
44                         break;
45                     else if (!visit[buf[j][p]-'A'])
46                         ++sum;
47                 }
48                 visit[buf[i][k]-'A'] = true;
49             }
50             dis[i][j] = dis[j][i] = sum;
51         }
52     }
53 }
54 
55 int main() {
56     int n;
57     int i, j, k, tmp;
58     int    v, mmin, sum;
59     string s;
60     
61     ios::sync_with_stdio(false);
62     
63     #ifndef ONLINE_JUDGE
64         freopen("data.in", "r", stdin);
65         freopen("data.out", "w", stdout);
66     #endif
67     
68     init();
69     while (cin>>n && n) {
70         for (i=0; i<n; ++i) {
71             cin >>s;
72             a[i] = tb[s];
73         }
74         mmin = INF;
75         for (i=0; i<nn; ++i) {
76             sum = 0;
77             for (j=0; j<n; ++j)
78                 sum += dis[i][a[j]];
79             if (sum < mmin) {
80                 v = i;
81                 mmin = sum;
82             }
83         }
84         printf("%s is the median ranking with value %d.\n", buf[v].c_str(), mmin);
85     }
86     
87     return 0;
88 }

 

posted on 2015-01-25 16:16  Bombe  阅读(186)  评论(0编辑  收藏  举报

导航