这回自己的代码是彻彻底底用不了……原因为以下:
- map自动排序……但是我没有搞明白是通过key排序!而不是我想的value!!
- map查询的时候不能找到key对应的下标!只能通过遍历查到!!!
对于STL库的数据结构不是很了解哈……等下抄完别人的代码总结一下map的用法好了QAQ
#include <iostream>
#include <cstring>
#include <algorithm>
#include <map>
#include <vector>
using namespace std;
const int N = 2e3 + 10;
int n, m;
struct student {
string name;
int rank;
string w;
int C, M, E, A;
}s[N];
map<string, int> av;
map<string, int> cv;
map<string, int> mv;
map<string, int> ev;
int main()
{
cin >> n >> m;
for (int i = 0; i < n; i ++ ) {
cin >> s[i].name >> s[i].C >> s[i].M >> s[i].E >> s[i].A;
cv[s[i].name] = s[i].C;
mv[s[i].name] = s[i].M;
ev[s[i].name] = s[i].E;
av[s[i].name] = s[i].A;
}
for (int i = 0; i < m; i ++ ) {
string ask;
cin >> ask;
auto ita = av.find(ask);
if(ita == av.end()) {
printf("N/A\n");
continue;
}
auto itc = cv.find(ask);
auto itm = mv.find(ask);
auto ite = ev.find(ask);
auto max1 = ita - av.begin();
int select1;
if(ita - av.begin() >= itc - cv.begin()) {
select1 = 1;
}
else select1 = 2;
max1 = max(ita - av.begin(), itc - cv.begin());
auto max2 = itm - mv.begin();
int select2;
if(itm - mv.begin() >= ite - ev.begin()) {
select2 = 3;
}
else select2 = 4;
max2 = max(itm - mv.begin(), ite - ev.begin());
if(max2 > max1) {
select1 = select2;
}
cout << max1 << " ";
if(select1 == 1) cout << "A" << endl;
else if(select1 == 2) cout << "C" << endl;
else if(select1 == 3) cout << "M" << endl;
else if(select1 == 4) cout << "E" << endl;
}
return 0;
}
以下是抄的y总的代码:
#include <iostream>
#include <cstring>
#include <algorithm>
#include <map>
#include <vector>
#include <unordered_map>
#include <cmath>
using namespace std;
unordered_map<string, vector<int>> grades;
vector<int> q[4];
int get_rank(vector<int> &a, int x) {
int l = 0, r = a.size() - 1;
while(l < r) {
int mid = (l + r + 1) >> 1;
if(a[mid] <= x) l = mid;
else r = mid - 1;
}
return a.size() - r;
}
int main()
{
int n, m;
cin >> n >> m;
for (int i = 0; i < n; i ++ ) {
string id;
int t[4] = {0};
cin >> id;
for (int j = 1; j < 4; j ++ ) {
cin >> t[j];
t[0] += t[j];
}
t[0] = round(t[0] / 3.0);
for (int j = 0; j < 4; j ++ ) {
q[j].push_back(t[j]);
grades[id].push_back(t[j]);
}
}
for (int i = 0; i < 4; i ++ ) sort(q[i].begin(), q[i].end());
char names[] = "ACME";
while(m -- ) {
string id;
cin >> id;
if(grades.count(id) == 0) puts("N/A");
else {
int res = n + 1;
char c;
for (int i = 0; i < 4; i ++ ) {
int rank = get_rank(q[i], grades[id][i]);
if(rank < res) {
res = rank;
c = names[i];
}
}
cout << res << " " << c << endl;
}
}
return 0;
}