题目描述
链接
给出一组学生的准考证号和成绩,准考证号包含了等级(乙甲顶),考场号,日期,和个人编号信息,并有三种查询方式
查询一:给出考试等级,找出该等级的考生,按照成绩降序,准考证升序排序
查询二:给出考场号,统计该考场的考生数量和总得分
查询三:给出考试日期,查询改日期下所有考场的考试人数,按照人数降序,考场号升序排序
分析
- 查询一和查询二都是可以直接遍历做到的!!!直接遍历,不用存中间状态!!!否则用map超时
- 查询三用unordered_map
- 注意!!不需要提前把所有都提取出来,变成string-->map<>这样,反而更慢,直接来一个查询,保存一个结果,再查多好!!!
- 当然非要用这个时,注意先建个tmp,再把值赋给最外层的map
- 可以用string.c_str()将string变成char*,可以用printf,不用cout,从而更快
#include<bits/stdc++.h> using namespace std; unordered_map<string, unordered_map<int, int> > t3; struct node{ string s; int num; }; int n,m; vector<node> a; bool cmp1(node &x, node &y){ if(x.num == y.num) return x.s<y.s; return x.num > y.num; } bool cmp2(pair<int,int> &x, pair<int,int> &y){ if(x.second == y.second) return x.first < y.first; return x.first > y.first; } void solve(){ for(int j=0;j<n;j++){ string date = a[j].s.substr(4,6); string site = a[j].s.substr(1,3); if(t3.find(date) == t3.end()){ unordered_map<int,int> tmp; //建一个临时的!!! t3[date] = tmp; } t3[date][stoi(site)]++; } } int main(){ scanf("%d%d",&n,&m); a.resize(n); for(int i=0;i<n;i++){ cin>>a[i].s>>a[i].num; } sort(a.begin(),a.end(),cmp1); solve(); for(int i=1;i<=m;i++){ int type; string s; cin>>type>>s; cout<<"Case "<<i<<": "<<type<<" "<<s<<endl; if(type == 1){ bool flag = 0; for(int j=0;j<n;j++){ if(a[j].s.substr(0,1) == s){ cout<<a[j].s<<" "<<a[j].num<<endl; flag = 1; } } if(!flag) printf("NA\n"); }else if(type == 2){ bool flag = 0; int cnt = 0, sum = 0; for(int j=0;j<n;j++){ if(a[j].s.substr(1,3) == s){ cnt++; sum += a[j].num; flag = 1; } } if(flag) printf("%d %d\n", cnt, sum); else printf("NA\n"); }else if(type == 3){ if(t3.find(s) == t3.end()){ cout<<"NA"<<endl; continue; } //unordered_map 更快! unordered_map<int, int> tmp = t3[s]; vector<pair<int,int> > ans; for(auto it=tmp.begin(); it!=tmp.end();it++){ ans.push_back(*it); } sort(ans.begin(),ans.end(),cmp2); for(int j=0;j<ans.size();j++){ //使用c_str()将字符串转为char*,避免使用cout超时 cout<<ans[j].first<<" "<<ans[j].second<<endl; } } } }
好代码
#include <iostream> #include <vector> #include <unordered_map> #include <algorithm> using namespace std; struct node { string t; int value; }; bool cmp(const node &a, const node &b) { return a.value != b.value ? a.value > b.value : a.t < b.t; } int main() { int n, k, num; string s; cin >> n >> k; vector<node> v(n); for (int i = 0; i < n; i++) cin >> v[i].t >> v[i].value; for (int i = 1; i <= k; i++) { cin >> num >> s; printf("Case %d: %d %s\n", i, num, s.c_str()); vector<node> ans; int cnt = 0, sum = 0; if (num == 1) { for (int j = 0; j < n; j++) if (v[j].t[0] == s[0]) ans.push_back(v[j]); } else if (num == 2) { for (int j = 0; j < n; j++) { if (v[j].t.substr(1, 3) == s) { cnt++; sum += v[j].value; } } if (cnt != 0) printf("%d %d\n", cnt, sum); } else if (num == 3) { unordered_map<string, int> m; for (int j = 0; j < n; j++) if (v[j].t.substr(4, 6) == s) m[v[j].t.substr(1, 3)]++; for (auto it : m) ans.push_back({it.first, it.second}); } sort(ans.begin(), ans.end(),cmp); for (int j = 0; j < ans.size(); j++) printf("%s %d\n", ans[j].t.c_str(), ans[j].value); if (((num == 1 || num == 3) && ans.size() == 0) || (num == 2 && cnt == 0)) printf("NA\n"); } return 0; }