首页 > 代码库 > 【HDOJ】3789 奥运排序问题

【HDOJ】3789 奥运排序问题

写了个函数指针,这题目很水,但是佷烦。

 1 #include <iostream>
 2 #include <cstring>
 3 #include <cstdio>
 4 #include <cstdlib>
 5 using namespace std;
 6 
 7 #define MAXN 305
 8 
 9 typedef struct {
10     int id, g, j;
11     float gp, jp;
12 } count_st;
13 
14 typedef struct {
15     int r, id;
16 } rank_st;
17 
18 count_st counts[MAXN], buf[MAXN];
19 rank_st rank[4][MAXN];
20 int ids[MAXN];
21 int n, m;
22 
23 int compg(const void *a, const void *b) {
24     return ((count_st *)b)->g - ((count_st *)a)->g;
25 }
26 
27 int compj(const void *a, const void *b) {
28     return ((count_st *)b)->j - ((count_st *)a)->j;
29 }
30 
31 int compgp(const void *a, const void *b) {
32     return ((count_st *)b)->gp>((count_st *)a)->gp ? 1:-1;
33 }
34 
35 int compjp(const void *a, const void *b) {
36     return ((count_st *)b)->jp>((count_st *)a)->jp ? 1:-1;
37 }
38 
39 void f(rank_st *a, int sel, int (*cmp)(const void*, const void *)) {
40     int i, j;
41     qsort(counts, m, sizeof(count_st), cmp);
42     for (i=0, j=0; i<m; ++i) {
43         if (sel == 0)
44             if (i && counts[i].g != counts[i-1].g)
45                 j = i;
46         if (sel == 1)
47             if (i && counts[i].j != counts[i-1].j)
48                 j = i;
49         if (sel == 2)
50             if (i && counts[i].gp != counts[i-1].gp)
51                 j = i;
52         if (sel == 3)
53             if (i && counts[i].jp != counts[i-1].jp)
54                 j = i;
55         (a+i)->id = counts[i].id;
56         (a+i)->r = j;
57     }
58 }
59 
60 int main() {
61     int i, j, k, v, min;
62     float dp;
63 
64     while (scanf("%d %d",&n,&m) != EOF) {
65         for (i=0; i<n; ++i) {
66             buf[i].id = i;
67             scanf("%d %d %f", &buf[i].g, &buf[i].j, &dp);
68             buf[i].gp = buf[i].g / dp;
69             buf[i].jp = buf[i].j / dp;
70         }
71         for (i=0; i<m; ++i) {
72             scanf("%d", &j);
73             counts[i] = buf[j];
74             ids[i] = j;
75         }
76         f(rank[0], 0, &compg);
77         f(rank[1], 1, &compj);
78         f(rank[2], 2, &compgp);
79         f(rank[3], 3, &compjp);
80         for (i=0; i<m; ++i) {
81             min = MAXN;
82             for (j=0; j<4; ++j) {
83                 for (k=0; k<m; ++k) {
84                     if (rank[j][k].id == ids[i]) {
85                         if (rank[j][k].r < min) {
86                             v = j;
87                             min = rank[j][k].r;
88                         }
89                         break;
90                     }
91                 }
92             }
93             printf("%d:%d\n",min+1,v+1);
94         }
95         printf("\n");
96     }
97 
98     return 0;
99 }