upper_bound
upper_bound
寻找第一个大于x的位置
#pragma GCC optimize(2)
#include <bits/stdc++.h>
using namespace std;
string id;
unordered_map<string, vector<int>> records;
vector<int> grade[4]; // A C M E
int main() {
int n, m;
cin >> n >> m;
for(int i = 0; i < n; i ++) {
int tmp[4] = {0};
cin >> id;
for(int j = 1; j < 4; j ++){
cin >> tmp[j];
tmp[0] += tmp[j];
}
tmp[0] = round(tmp[0] / 3.0);
for(int j = 0; j < 4; j ++){
grade[j].push_back(tmp[j]);
records[id].push_back(tmp[j]);
}
}
for(int i = 0; i < 4; i ++) sort(grade[i].begin(), grade[i].end());
string names = "ACME";
while(m --) {
cin >> id;
if(records.count(id) == 0) puts("N/A");
else{
int res = n + 1; char ch;
for(int i = 0; i < 4; i ++) {
int rank = 1 + n - (upper_bound(grade[i].begin(), grade[i].end(), records[id][i]) - grade[i].begin());
if (rank < res) res = rank, ch = names[i];
}
cout << res << " " << ch << endl;
}
}
return 0;
}
lower_bound做法
- 写一个cmp倒序排vector
bool cmp(int a, int b) {return a > b;}
- 找
rank
时不用n - - lower_bound()加入第4个参数
greater<int>()