pat甲级1012

1012 The Best Rank (25)(25 分)

To evaluate the performance of our first year CS majored students, we consider their grades of three courses only: C - C Programming Language, M - Mathematics (Calculus or Linear Algebra), and E - English. At the mean time, we encourage students by emphasizing on their best ranks -- that is, among the four ranks with respect to the three courses and the average grade, we print the best rank for each student.

For example, The grades of C, M, E and A - Average of 4 students are given as the following:

StudentID  C  M  E  A
310101     98 85 88 90
310102     70 95 88 84
310103     82 87 94 88
310104     91 91 91 91

Then the best ranks for all the students are No.1 since the 1st one has done the best in C Programming Language, while the 2nd one in Mathematics, the 3rd one in English, and the last one in average.

Input

Each input file contains one test case. Each case starts with a line containing 2 numbers N and M (<=2000), which are the total number of students, and the number of students who would check their ranks, respectively. Then N lines follow, each contains a student ID which is a string of 6 digits, followed by the three integer grades (in the range of [0, 100]) of that student in the order of C, M and E. Then there are M lines, each containing a student ID.

Output

For each of the M students, print in one line the best rank for him/her, and the symbol of the corresponding rank, separated by a space.

The priorities of the ranking methods are ordered as A > C > M > E. Hence if there are two or more ways for a student to obtain the same best rank, output the one with the highest priority.

If a student is not on the grading list, simply output "N/A".

Sample Input

5 6
310101 98 85 88
310102 70 95 88
310103 82 87 94
310104 91 91 91
310105 85 90 90
310101
310102
310103
310104
310105
999999

Sample Output

1 C
1 M
1 E
1 A
3 A
N/A

由于C,M,E三科分数均为[0, 100]整数,因此可用数组cnt存储每个分数的人数,然后累加就可以得到每个分数对应的名次。
而平均分不是整数,先将平均分排序,然后用二分查找的方法找出其对应的名次。
 1 #include <iostream>
 2 #include <vector>
 3 #include <algorithm>
 4 #include <map>
 5 using namespace std;
 6 
 7 struct grade
 8 {
 9     int s[3];
10     double A;
11 };
12 int cnt[3][101];
13 int cnt2[3][101];
14 
15 int avgRank(vector<double> avg, double d);
16 
17 int main()
18 {
19     int n, m;
20     cin >> n >> m;
21     map<int, grade> mapp;
22     vector<double> avg;
23 
24     int id, i, j;
25     grade g;
26     for (i = 0; i < n; i++)
27     {
28         cin >> id;
29         g.A = 0;
30         for (j = 0; j < 3; j++)
31         {
32             cin >> g.s[j];
33             g.A += (double)g.s[j];
34             cnt[j][g.s[j]]++;
35         }
36         g.A /= 3;
37         avg.push_back(g.A);
38         mapp[id] = g;
39     }
40 
41     cnt2[0][100] = cnt2[1][100] = cnt2[2][100] = 1;
42     for (i = 99; i >= 0; i--)
43     {
44         for (j = 0; j < 3; j++)
45             cnt2[j][i] = cnt2[j][i + 1] + cnt[j][i + 1];
46     }
47 
48     sort(avg.begin(), avg.end());
49 
50     int min, s, t;
51     char arr[4] = { 'C', 'M', 'E', 'A' };
52     for (i = 0; i < m; i++)
53     {
54         min = 3000;
55         cin >> id;
56         if (mapp.find(id) == mapp.end())
57         {
58             cout << "N/A" << endl;
59             continue;
60         }
61         for (j = 0; j < 3; j++)
62         {
63             t = cnt2[j][mapp[id].s[j]];
64             if ( t < min)
65             {
66                 min = t;
67                 s = j;
68             }
69         }
70         t = avgRank(avg, mapp[id].A);
71         if (t <= min)
72         {
73             min = t;
74             s = 3;
75         }
76         cout << min << " " << arr[s] << endl;
77     }
78     return 0;
79 }
80 
81 int avgRank(vector<double> avg, double d)
82 {
83     int mid = -1, i = 0, j = avg.size() - 1;
84     while (i <= j)
85     {
86         mid = (i + j) / 2;
87         if (d > avg[mid])
88             i = mid + 1;
89         else if (d == avg[mid])
90             break;
91         else
92             j = mid - 1;
93     }
94     return avg.size() - mid;
95 }

提交时发现直接把平均分四舍五入取整也可以通过:

 1 #include <iostream>
 2 
 3 #include <map>
 4 using namespace std;
 5 
 6 struct grade
 7 {
 8     int s[4];
 9 };
10 
11 int cnt[4][101];
12 int cnt2[4][101];
13 
14 int main()
15 {
16     int n, m;
17     cin >> n >> m;
18     map<int, grade> mapp;
19     
20     int id, i, j;
21     grade g;
22     for (i = 0; i < n; i++)
23     {
24         cin >> id;
25         g.s[0] = 0;
26         for (j = 1; j < 4; j++)
27         {
28             cin >> g.s[j];
29             g.s[0] += g.s[j];
30             cnt[j][g.s[j]]++;
31         }
32         g.s[0] = g.s[0] / 3.0 + 0.5;
33         cnt[0][g.s[0]]++;
34         mapp[id] = g;
35     }
36 
37     for (i = 0; i < 4; i++)
38     {
39         cnt2[i][100] = 1;
40         for (j = 99; j >= 0; j--)
41             cnt2[i][j] = cnt2[i][j + 1] + cnt[i][j + 1];
42     }
43     int min, s, t;
44     char arr[4] = { 'A', 'C', 'M', 'E' };
45     for (i = 0; i < m; i++)
46     {
47         min = 3000;
48         cin >> id;
49         if (mapp.find(id) == mapp.end())
50         {
51             cout << "N/A" << endl;
52             continue;
53         }
54         for (j = 0; j < 4; j++)
55         {
56             t = cnt2[j][mapp[id].s[j]];
57             if ( t < min)
58             {
59                 min = t;
60                 s = j;
61             }
62         }
63         cout << min << " " << arr[s] << endl;
64     }
65     return 0;
66 }


猜你喜欢

转载自www.cnblogs.com/lxc1910/p/9472632.html