题解
大数据处理 |
---|
这样的大数据的题,首先要确定一个方便的数据结构 |
我来说一下我的数据结构,当然不是最好的,我见我老师的代码比我快挺多 |
数据结构分析: |
1. “类型 为 1 表示要求按分数非升序输出某个指定级别的考生的成绩,对应的 指令 则给出代表指定级别的字母;” |
map<char, vector<pair<string, int>>> A; |
让级别对应一个集合,集合里存放这个人的id和分数 |
2. “类型 为 2 表示要求将某指定考场的考生人数和总分统计输出,对应的 指令 则给出指定考场的编号;” |
map<string, pair<int, int>> B; |
考场对应人数和总分 |
3. “为 3 表示要求将某指定日期的考生人数分考场统计输出,对应的 指令 则给出指定日期,格式与准考证上日期相同。” |
struct node
{
map<string, int> rap;
};
map<string, node> C;
这个稍微难点,我认为 |
---|
日期对应考场和人数,node里的rap是考场对应人数 |
因为人数是随着数据变化的,而考场号却不变,必须让不边的对应变化的。但是之后的排序就有点麻烦,得先拿出到vector里,在sort排序 |
C语言风格的字符串 |
---|
本来我的这个结构是超时的,我用的是cin,cout |
然后换成了scanf和printf就OK了 |
但是string,并不能直接换啊。我的代码中用的是C语言风格的string |
具体更详细的用法可以留言或者私聊 |
Code
#include <iostream>
#include <map>
#include <set>
#include <algorithm>
#include <vector>
using namespace std;
map<char, vector<pair<string, int>>> A;
map<string, pair<int, int>> B;
struct node
{
map<string, int> rap;
};
map<string, node> C;
bool cmp(pair<string, int> e1, pair<string, int> e2)
{
if (e1.second == e2.second)
return e1.first < e2.first;
return e1.second > e2.second;
}
int main()
{
int N, M;
scanf("%d %d\n", &N, &M);
string s;
int x;
s.resize(13);
while (N--)
{
scanf("%s%d", &s[0], &x);
char c = s[0];
string num = s.substr(1, 3);
string data = s.substr(4, 6);
A[c].push_back({s, x});
B[num].first++;
B[num].second += x;
++C[data].rap[num];
}
char c;
for (int i = 1; i <= M; i++)
{
scanf("%d", &x);
printf("Case %d: %d ", i, x);
if (x == 1)
{
scanf(" %c", &c);
printf("%c\n", c);
if (A.count(c) == 0)
{
cout << "NA\n";
continue;
}
sort(A[c].begin(), A[c].end(), cmp);
for (auto e : A[c])
printf("%s %d\n", e.first.c_str(), e.second);
}
else if (x == 2)
{
cin >> s;
printf("%s\n", s.c_str());
if (B.count(s) == 0)
{
cout << "NA\n";
continue;
}
printf("%d %d\n", B[s].first, B[s].second);
}
else
{
cin >> s;
printf("%s\n", s.c_str());
if (C.count(s) == 0)
{
cout << "NA\n";
continue;
}
vector<pair<string, int>> v;
for (auto e : C[s].rap)
{
v.push_back(e);
}
sort(v.begin(), v.end(), cmp);
for (auto e : v)
printf("%s %d\n", e.first.c_str(), e.second);
}
}
return 0;
}