-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathLv2_순위검색.cpp
131 lines (109 loc) · 3.41 KB
/
Lv2_순위검색.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
#include <string>
#include <vector>
#include <iostream>
#include <sstream>
#include <regex>
#include <algorithm>
#include <unordered_map>
using namespace std;
struct user {
string code;
int score;
};
user users[50001];
vector<string> candidates;
unordered_map<string, pair<int, int>> m;
const string ops[4][3] = { {"java", "python", "cpp"}, {"frontend","backend"},{"junior","senior"},{"pizza","chicken"}};
bool cmp(user &u1, user &u2) {
if (u1.code < u2.code)
return true;
else if (u1.code == u2.code) {
if (u1.score < u2.score)
return true;
else
return false;
}
else
return false;
}
void getCandidate(string s[], string code, int idx){
if (idx > 3) {
candidates.push_back(code);
return;
}
if (s[idx] != "-") {
getCandidate(s, code + s[idx], idx + 1);
}
else {
int end = 2;
if (idx == 0)
end = 3;
for (int i = 0; i < end; ++i)
getCandidate(s, code + ops[idx][i], idx + 1);
}
}
vector<int> solution(vector<string> info, vector<string> query) {
vector<int> answer;
int row = info.size();
for (int i = 0; i < info.size(); ++i) {
string s[5];
istringstream sst(info[i]);
sst >> s[0] >> s[1] >> s[2] >> s[3] >> s[4];
string code;
for (int i = 0; i < 4; ++i)
code += s[i];
users[i] = {code, stoi(s[4])};
}
sort(users, users + row, cmp);
int s=0, e=0;
string prev = users[0].code;
for (int i = 0; i < row; ++i) {
if (prev != users[i].code) {
e = i - 1;
if(e!=-1)
m.insert({ prev, { s,e } });
prev = users[i].code;
s = i;
e = i;
}
}
m.insert({ prev, { s,row-1 } });
for (auto a : query) {
string b = regex_replace(a,regex("and "),"");
string s[5];
istringstream sst(b);
sst >> s[0] >> s[1] >> s[2] >> s[3] >> s[4];
candidates.clear();
getCandidate(s, "", 0);
int count = 0;
int want = stoi(s[4]);
for (auto b : candidates) {
if (m.count(b) > 0) {
pair<int, int> pos = m[b];
int left = pos.first;
int right = pos.second;
int mid, lower_bnd=-1;
while (left <= right){
mid = (left + right) >> 1;
if (users[mid].score >= want){
lower_bnd = mid;
right = mid - 1;
}
else left = mid + 1;
}
if(lower_bnd!=-1)
count += pos.second-lower_bnd+1;
}
}
answer.push_back(count);
}
return answer;
}
int main() {
vector<string> info = { "java backend junior pizza 150", "python frontend senior chicken 210", "python frontend senior chicken 150", "cpp backend senior pizza 260", "java backend junior chicken 80", "python backend senior chicken 50" };
vector<string> query = { "java and backend and junior and pizza 100", "python and frontend and senior and chicken 200", "cpp and - and senior and pizza 250", "- and backend and senior and - 150", "- and - and - and chicken 100", "- and - and - and - 150" };
vector<int> answer = solution(info, query);
for (auto a : answer)
cout << a << " ";
return 0;
}