题目链接
题解结构体排序。
我是写的四个不同的cmp函数,分开进行四次排序,每次更新最小排名,同时记录学科;
柳巨是将每个人的四个分数保存成了一个数组,写了一个函数,通过下标控制对哪个学科的成绩进行排序,可以通过循环来实现对每个学科的排序,每次更新最小排名,同时记录学科。
好看的AC代码:
#include
using namespace std;
const int N = 1e7+10;
string mp = "ACME";
int flag;
struct student {
int id, score[4], rk[4];
} s[N];
int best_rk[N], best_class[N];
bool cmp (student x, student y) {
return x.score[flag] > y.score[flag];
}
int main()
{
int n, m;
cin >> n >> m;
for (int i = 0;i < n;i ++) {
cin >> s[i].id >> s[i].score[1] >> s[i].score[2] >> s[i].score[3];
s[i].score[0] = int (1.0 * (s[i].score[1] + s[i].score[2] + s[i].score[3]) / 3);
}
for (flag = 0;flag < 4;flag ++) {
sort (s, s + n, cmp);
s[0].rk[flag] = 1;
if (!best_rk[s[0].id] || s[0].rk[flag] < best_rk[s[0].id])
best_rk[s[0].id] = s[0].rk[flag],
best_class[s[0].id] = flag;
for (int i = 1;i < n;i ++) {
int id = s[i].id;
if (s[i].score[flag] == s[i-1].score[flag]) s[i].rk[flag] = s[i-1].rk[flag];
else s[i].rk[flag] = i + 1;
if (!best_rk[id] || s[i].rk[flag] < best_rk[id])
best_rk[id] = s[i].rk[flag],
best_class[id] = flag;
}
}
while (m --) {
int id;
cin >> id;
if (!best_rk[id]) cout << "N/A" << endl;
else cout << best_rk[id] << ' ' << mp[best_class[id]] << endl;
}
return 0;
}
丑陋的AC代码:
#include
using namespace std;
const int N = 1e7+1;
struct student {
int id, c, m, e, a;
} s[N];
bool cmp_a (student x, student y) {
return x.a > y.a;
}
bool cmp_c (student x, student y) {
return x.c > y.c;
}
bool cmp_m (student x, student y) {
return x.m > y.m;
}
bool cmp_e (student x, student y) {
return x.e > y.e;
}
int rk_a[N], rk_c[N], rk_m[N], rk_e[N], rk[N];
map <int, char> rk_class;
int main()
{
int n, m;
cin >> n >> m;
for (int i = 0;i < n;i ++) {
cin >> s[i].id >> s[i].c >> s[i].m >> s[i].e;
s[i].a = int ((s[i].c + s[i].m + s[i].e) * 1.0 / 3);
}
sort (s, s + n, cmp_a);
rk_a[s[0].id] = 1;
rk[s[0].id] = 1;
rk_class[s[0].id] = 'A';
for (int i = 1;i < n;i ++) {
if (s[i].a == s[i-1].a) rk_a[s[i].id] = rk_a[s[i-1].id];
else rk_a[s[i].id] = i+1;
rk[s[i].id] = rk_a[s[i].id];
rk_class[s[i].id] = 'A';
}
sort (s, s + n, cmp_c);
rk_c[s[0].id] = 1;
if (rk_c[s[0].id] < rk[s[0].id]) {
rk[s[0].id] = rk_c[s[0].id];
rk_class[s[0].id] = 'C';
}
for (int i = 1;i < n;i ++) {
if (s[i].c == s[i-1].c) rk_c[s[i].id] = rk_c[s[i-1].id];
else rk_c[s[i].id] = i+1;
if (rk_c[s[i].id] < rk[s[i].id]) {
rk[s[i].id] = rk_c[s[i].id];
rk_class[s[i].id] = 'C';
}
}
sort (s, s + n, cmp_m);
rk_m[s[0].id] = 1;
if (rk_m[s[0].id] < rk[s[0].id]) {
rk[s[0].id] = rk_m[s[0].id];
rk_class[s[0].id] = 'M';
}
for (int i = 1;i < n;i ++) {
if (s[i].m == s[i-1].m) rk_m[s[i].id] = rk_m[s[i-1].id];
else rk_m[s[i].id] = i+1;
if (rk_m[s[i].id] < rk[s[i].id]) {
rk[s[i].id] = rk_m[s[i].id];
rk_class[s[i].id] = 'M';
}
}
sort (s, s + n, cmp_e);
rk_e[s[0].id] = 1;
if (rk_e[s[0].id] < rk[s[0].id]) {
rk[s[0].id] = rk_e[s[0].id];
rk_class[s[0].id] = 'E';
}
for (int i = 1;i < n;i ++) {
if (s[i].e == s[i-1].e) rk_e[s[i].id] = rk_e[s[i-1].id];
else rk_e[s[i].id] = i+1;
if (rk_e[s[i].id] < rk[s[i].id]) {
rk[s[i].id] = rk_e[s[i].id];
rk_class[s[i].id] = 'E';
}
}
while (m --) {
int id;
cin >> id;
if (!rk_class[id]) cout << "N/A" << endl;
else cout << rk[id] << ' ' << rk_class[id] << endl;
}
return 0;
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)