-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHomeWork04_online_election_binary_research.cpp
More file actions
63 lines (49 loc) · 1.81 KB
/
HomeWork04_online_election_binary_research.cpp
File metadata and controls
63 lines (49 loc) · 1.81 KB
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
class TopVotedCandidate {
public:
unordered_map<int,int> time_index_to_best_candidate;
vector<int> p;
int N = 0;
TopVotedCandidate(vector<int>& persons, vector<int>& times) {
N = times.size();
vector<int> candidate_to_tickets = vector<int>(5000,0);
time_index_to_best_candidate[0] = persons[0];
candidate_to_tickets[persons[0]]++;
p = times;
for(int i =1; i < times.size(); i++){
candidate_to_tickets[persons[i]]++;
if(time_index_to_best_candidate[i-1] != persons[i] && candidate_to_tickets[persons[i]] >= candidate_to_tickets[time_index_to_best_candidate[i-1]]){
time_index_to_best_candidate[i] = persons[i];
}
else{
time_index_to_best_candidate[i] = time_index_to_best_candidate[i-1];
}
//cout <<" candi: " <<persons[i] << " tick: "<<candidate_to_tickets[persons[i]] << endl;
//cout << time_index_to_best_candidate[i] << " " << endl;
}
}
int q(int t) {
//int i = 0;
if(t >= p.back()) return time_index_to_best_candidate[N-1];
int left = 0;
int right = N-1;
while(left < right){
int mid = (left + right)>>1;
if(p[mid] >= t){
right = mid;
}
else{
left = mid+1;
}
}
if(t != p[right]) right--;
//cout << right << " ";
//if(i != N - 1 && t == p[i+1] ) i = i + 1;
//cout << i << " ";
return time_index_to_best_candidate[right];
}
};
/**
* Your TopVotedCandidate object will be instantiated and called as such:
* TopVotedCandidate* obj = new TopVotedCandidate(persons, times);
* int param_1 = obj->q(t);
*/