fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int binary_search(vector<int> times, int search, int low, int high) {
  5. if (low > high) {
  6. return high;
  7. }
  8. int mid = (low + high)/2;
  9. if (times[mid] == search) return mid;
  10. else if (times[mid] > search) return binary_search(times, search, low, mid-1);
  11. else return binary_search(times, search, mid+1, high);
  12. }
  13.  
  14. void returnLeader(vector<int> votes, vector<int> times, vector<int> instances) {
  15. vector<int> tmp;
  16. vector<int> cand_votes(votes.size(), 0);
  17. vector<int> result;
  18. int maxVotes = INT_MIN, maxVoteCandidate = votes[0];
  19. for(int i=0;i<times.size();i++) {
  20. cand_votes[votes[i]]++;
  21. if (maxVotes <= cand_votes[votes[i]]) {
  22. maxVotes = cand_votes[votes[i]];
  23. maxVoteCandidate = votes[i];
  24. }
  25. tmp.push_back(maxVoteCandidate);
  26. }
  27. for(int i=0;i<instances.size();i++) {
  28. int idx = binary_search(times, instances[i], 0, times.size()-1);
  29. cout << idx << endl;
  30. cout << tmp[idx] << " ";
  31. }
  32. return;
  33. }
  34.  
  35. int main() {
  36. // your code goes here
  37. vector<int> votes = {0,1};
  38. vector<int> times = {20,25};
  39. vector<int> instances = {17};
  40. returnLeader(votes, times, instances);
  41. return 0;
  42. }
Success #stdin #stdout 0s 4312KB
stdin
Standard input is empty
stdout
-1
0