-
Notifications
You must be signed in to change notification settings - Fork 0
/
2019.08.05.cpp
202 lines (197 loc) · 3.87 KB
/
2019.08.05.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<iterator>
using namespace std;
class Student
{
public:
string id;
char level;
int place;
string date;
int code;
int score;
public:
Student() :id(""), level('\0'), place(0), date(""), code(0), score(0){}
Student(string c) :id(c)
{
level = id[0];
place = stoi(id.substr(1, 3));
date = id.substr(4, 6);
code = stoi(id.substr(10, 3));
}
friend bool cmpScore(const Student& s1, const Student& s2);
~Student(){}
};
bool cmpScore(const Student& s1, const Student& s2)
{
if (s1.score > s2.score)
return true;
else if (s1.score == s2.score)
return s1.id < s2.id;
else
return false;
}
class myPair
{
private:
int place;
int nums;
public:
myPair() :place(0), nums(0){}
myPair(int p, int n) :place(p), nums(n){}
friend bool cmpPair(const myPair& p1, const myPair& p2);
friend ostream& operator<<(ostream& os, const myPair& mp);
};
bool cmpPair(const myPair& p1, const myPair& p2)
{
if (p1.nums > p2.nums)
return true;
else if (p1.nums == p2.nums)
return p1.place < p2.place;
else
return false;
}
ostream& operator<<(ostream& os, const myPair& mp)
{
os << mp.place << " " << mp.nums << endl;
return os;
}
class IQuery
{
public:
IQuery(){}
virtual void process(const vector<Student>& students,int casenum) = 0;
virtual ~IQuery(){}
};
class Query1 :public IQuery
{
private:
char param;
public:
Query1(char p) :param(p){}
void process(const vector<Student>& students, int casenum)
{
vector<Student> ans;
for (size_t i = 0; i < students.size(); ++i)
{
if (students[i].level == param)
ans.push_back(students[i]);
}
sort(ans.begin(), ans.end(), cmpScore);
cout << "Case "<<casenum<<": 1 " << param << endl;
if (ans.size() == 0)
cout << "NA" << endl;
for (size_t i = 0; i < ans.size(); ++i)
cout << ans[i].id << " " << ans[i].score << endl;
}
~Query1(){}
};
class Query2 :public IQuery
{
private:
int param;
public:
Query2(int p) :param(p){}
void process(const vector<Student>& students, int casenum)
{
int numbers=0, scores=0;
for (auto& x : students)
{
if (x.place == param)
++numbers, scores += x.score;
}
cout << "Case " << casenum << ": 2 " << param << endl;
if (numbers == 0)
cout << "NA" << endl;
else
cout << numbers << " " << scores << endl;
}
~Query2(){}
};
class Query3 :public IQuery
{
private:
string param;
public:
Query3(string p) :param(p){}
void process(const vector<Student>& students, int casenum)
{
int hash[899] = { 0 };
for (auto& x : students)
{
if (x.date == param)
++hash[x.place - 101];
}
vector<myPair> ans;
for (size_t i = 0; i < 899; ++i)
{
if (hash[i] != 0)
ans.push_back(myPair(i + 101, hash[i]));
}
sort(ans.begin(), ans.end(), cmpPair);
cout << "Case " << casenum << ": 3 " << param << endl;
if (ans.size() == 0)
cout << "NA" << endl;
else
copy(ans.begin(), ans.end(), ostream_iterator<myPair>(cout));
}
};
int main()
{
int N, M;
cin >> N >> M;
cin.get();
string studentID;
int score;
vector<Student> students(N);
for (int i = 0; i < N; ++i)
{
cin >> studentID >> score;
cin.get();
Student temp(studentID);
temp.score=score;
students[i] = temp;
}
int casenum;
IQuery* query = nullptr;
for (int i = 0; i < M; ++i)
{
cin >> casenum;
switch (casenum)
{
case 1:
{
cin.get();
char lev;
cin >> lev;
cin.get();
query = new Query1(lev);
break;
}
case 2:
{
int plce;
cin >> plce;
query = new Query2(plce);
break;
}
case 3:
{
string date;
cin.get();
cin >> date;
cin.get();
query = new Query3(date);
}
default:
break;
}
query->process(students, i + 1);
delete query;
query = nullptr;
}
return 0;
}