题意:
思路:
用v e c t o r < i n t > v 记录每个时刻的获胜者,对于每次询问都在t i m e s里二分查找小于等于该时刻的最大时刻,这时候的获胜者就是这次的答案,。
代码:
class TopVotedCandidate { public: vector<int>v,tt; map<int,int>mp; TopVotedCandidate(vector<int>& persons, vector<int>& times) { int n=persons.size(); int maxx=0,pos=5100; for(int i=0;i<n;i++){ mp[persons[i]]++; if(mp[persons[i]]>=maxx){ maxx=mp[persons[i]]; pos=persons[i]; } v.push_back(pos); // cout<<i<<"***"<<times[i]<<"*****"<<pos<<endl; tt.push_back(times[i]); } } int q(int t) { int ans=upper_bound(tt.begin(),tt.end(),t)-tt.begin()-1; //cout<<ans<<endl; return v[ans]; } }; /** * Your TopVotedCandidate object will be instantiated and called as such: * TopVotedCandidate* obj = new TopVotedCandidate(persons, times); * int param_1 = obj->q(t); */