当前位置: 首页 > wzjs >正文

西安网站优化平台长沙市公司

西安网站优化平台,长沙市公司,荆门住房建设厅网站,wordpress显示网站运行题目比较复杂,主要体现在流程很长,就是队列加二分的操作 难点1:深拷贝 主要问题1,看下面这段核心代码: int getCount(int destination, int startTime, int endTime) {auto times dest2times[destination];int ans …

题目比较复杂,主要体现在流程很长,就是队列加二分的操作

难点1:深拷贝
主要问题1,看下面这段核心代码:

    int getCount(int destination, int startTime, int endTime) {auto times = dest2times[destination];int ans = 0;auto lower_it = lower_bound(times.begin(), times.end(), startTime);size_t lower_idx = lower_it - times.begin();auto upper_it = upper_bound(times.begin(), times.end(), endTime);size_t upper_idx = upper_it - times.begin();ans = upper_idx - lower_idx;return ans;}

乍一看是 O ( l o g N ) O(logN) O(logN) 的操作,但实际上,第一步取出来的时候,这一步是深拷贝,就已经花费了 O ( N ) O(N) O(N) 的时间,所以,除了size可以深拷贝,在把较大的数据结构取出来时,一定要注意避免类似的操作。

难点2:二分
上述代码,都减去了times.begin(),其实是重复操作,用下面的代码更为简洁

    int getCount(int destination, int startTime, int endTime) {auto it1 = lower_bound(dest2times[destination].begin(), dest2times[destination].end(), startTime);auto it2 = upper_bound(dest2times[destination].begin(), dest2times[destination].end(), endTime);return it2-it1;}

upper_bound:找到第一个大于目标数的位置
lower_bound:找到第一个大于等于目标数的位置
直接用bound的返回值去减,就可以得到元素数目,若是想得到对应的索引,则需要去减begin()
在非强二分考察题目时,用bound是更为省时、简单的操作。

附ac代码

typedef tuple<int, int, int> T;
typedef deque<int> V;
class Router {
set<T> packets;
map<int, V> dest2times;
queue<T> q;
int limit;
public:Router(int memoryLimit) {limit = memoryLimit;}bool addPacket(int source, int destination, int timestamp) {T t1(source, destination, timestamp);if(packets.count(t1))return false;if(q.size() == limit){T t2 = q.front();dest2times[get<1>(t2)].pop_front();packets.erase(t2);q.pop();}dest2times[destination].push_back(timestamp);q.push(t1);packets.insert(t1);return true;}vector<int> forwardPacket() {if(q.size() == 0)return vector<int>{};T t2 = q.front();dest2times[get<1>(t2)].pop_front();packets.erase(t2);q.pop();return vector<int>{get<0>(t2), get<1>(t2), get<2>(t2)};}int findFirst(V& nums, int target){int n = nums.size();int l = 0, r = n-1;while(l<r){int mid = l + (r-l)/2;if(nums[mid] < target)l = mid+1;else r = mid;}return l;}int findLast(V& nums, int target){int n = nums.size();int l = 0, r = n-1;while(l<r){int mid = l + (r-l+1)/2;if(nums[mid] > target)r = mid-1;else l = mid;}return l;}int getCount(int destination, int startTime, int endTime) {int ans = 0;int start = findFirst(dest2times[destination], startTime);int end = findLast(dest2times[destination], endTime);if(dest2times[destination][start] >= startTime and dest2times[destination][end] <= endTime)ans = end-start+1;return ans;}
};/*** Your Router object will be instantiated and called as such:* Router* obj = new Router(memoryLimit);* bool param_1 = obj->addPacket(source,destination,timestamp);* vector<int> param_2 = obj->forwardPacket();* int param_3 = obj->getCount(destination,startTime,endTime);*/

文章转载自:

http://zbcK8s6j.hsrch.cn
http://3mzQkZlJ.hsrch.cn
http://w7AI0k0V.hsrch.cn
http://OsK48NEa.hsrch.cn
http://QlHMq6CS.hsrch.cn
http://CbNEpYOi.hsrch.cn
http://3aC3yBbS.hsrch.cn
http://nsqqRrv1.hsrch.cn
http://QcCHQv7B.hsrch.cn
http://rv3YbgXM.hsrch.cn
http://qeNoGuQ1.hsrch.cn
http://GkY2lwMu.hsrch.cn
http://QOHufxm6.hsrch.cn
http://i3JG6vi3.hsrch.cn
http://UCFE01HS.hsrch.cn
http://6uhVhNrf.hsrch.cn
http://Zlcw5z0r.hsrch.cn
http://ifThmRfH.hsrch.cn
http://xEKh5IHH.hsrch.cn
http://cidsDGrI.hsrch.cn
http://yirSyM8a.hsrch.cn
http://brK0kUhq.hsrch.cn
http://h11ef02P.hsrch.cn
http://SU4sDbGd.hsrch.cn
http://e7O17ill.hsrch.cn
http://lLRV28mG.hsrch.cn
http://TtWfqjTt.hsrch.cn
http://xbKjGYsf.hsrch.cn
http://ZFNQTswC.hsrch.cn
http://5Ire65E5.hsrch.cn
http://www.dtcms.com/wzjs/737246.html

相关文章:

  • 上海公司网站建设多少钱软件开发合同范本免费下载
  • 电子商务网站建设技术方案企业免费网站制作比较好的
  • 云凡济南网站建设开发企业网站优化关键词
  • 做网站老板不发工资我拿尾款学物联网工程后悔死了
  • 网站建设公司行业描述百度在成都有分公司吗
  • 网站域名区别吗宝塔 wordpress
  • 网站栏目分类呼市网站开发
  • 国外互联网资讯网站网站布局设计规则
  • 四川省住建厅官方网站网站备案阿里云流程
  • 网站建设会计宁波优化推广找哪家
  • 网站前端和后台威海seo优化公司
  • 海南网站建设平台适合程序员的wordpress主题
  • 长春h5建站模板黄骅市官网
  • 网站建设 中企动力 常州wordpress 背景音乐
  • 网站分享链接怎么做泰安城市建设吧
  • 外贸网站平台有几个wordpress禁主题
  • 专门做音箱的网站做网站租用那个服务器好
  • 做ppt的软件怎么下载网站如何编写一个网站
  • 合肥做网站mdyun前端开发的软件
  • 沙洋网站开发黄浦手机网站建设
  • 可以做淘宝客的网站文章资讯类网站模板
  • wordpress 总站模板哪里有网站建站公司
  • 漫画网站开发wordpress主题简
  • 2019年建设什么网站好单页面网站有哪些内容
  • 黑色大气网站企业个性化网站建设费用
  • 专业企业网站建设多少钱服务企业网站建设方案书前言
  • 可以写代码的网站微分销平台怎么样
  • 建站工具指北seo关键词怎么选择
  • 播放器网站怎么做电子商务专业就业前景好不好
  • 建设银行网站卡死网站分类导航代码