【算法题】2018今日头条编程题一
2017-08-23 11:47
471 查看
维持一个当前边界点的数组,按x从小到达排序,由于是边界点,故其y对应是从大到小排序的。
对于一个新加入的点,分别按x和y坐标值二分查找其在边界点数组中的位置:iter_x , iter_y
若iter_x == iter_y:
则直接插入新边界点
若iter_x < iter_y :
非边界点
若iter_x>iter_y:
删除iter_x 与iter_y之间的无效边界点,并插入新边界点
#include <stdio.h> #include <vector> #include <numeric> #include <algorithm> #include <iostream> #include <list> using namespace std; //#define debug_ int N; struct point { int x; int y; }; typedef vector<point>::iterator iter; vector<point> vec; void func() { vector<point> Bound; iter iter_x, iter_y; for (auto i = 0; i < N;++i) { iter_x = lower_bound(Bound.begin(), Bound.end(), vec[i], [](const point& lhs, const point& rhs){ return lhs.x < rhs.x; }); iter_y = lower_bound(Bound.begin(), Bound.end(), vec[i], [](const point& lhs, const point& rhs){ return lhs.y >= rhs.y; }); if (iter_x == iter_y) { Bound.insert(iter_x,vec[i]); continue; } if (iter_x<iter_y) { continue; } else { iter_x = Bound.erase(iter_y, iter_x); Bound.insert(iter_x, vec[i]); } } for (auto i = 0; i < Bound.size();++i) { cout << Bound[i].x << " " << Bound[i].y << endl; } } int main() { #ifdef debug_ N = 5; vec.resize(N); vec[0].x = 1; vec[0].y 4000 = 2; vec[1].x = 5; vec[1].y = 3; vec[2].x = 4; vec[2].y = 6; vec[3].x = 7; vec[3].y = 5; vec[4].x = 7; vec[4].y = 6; #else cin >> N; vec.resize(N); for (auto i = 0; i < N;++i) { cin>>vec[i].x; cin>>vec[i].y; } #endif func(); return 0; }
相关文章推荐
- 今日头条2018春季 算法实习生编程题 python
- 今日头条2018校招算法方向(第一批)编程题1(离散+树状数组)
- 牛客网--今日头条2018校招算法方向(第一批)编程题1、编程题2
- 今日头条2018校招算法方向(第一批)编程题2(枚举)
- 【算法题】2018今日头条编程题二
- 今日头条2018春季校园招聘研发岗位笔试编程题1
- 今日头条2018校招测试开发方向(第一、二、三、四批)编程题汇总 - 题解
- 今日头条2018校园招聘后端开发工程师(第二批)编程题 - 题解
- 今日头条2018春季校园招聘研发岗位笔试编程题第一、二题
- 今日头条2018春季校园招聘研发岗位笔试编程题4
- 【算法】今日头条2017编程题
- 今日头条2018春季校园招聘研发岗位笔试编程题2
- 今日头条2018春招-算法方向题解
- 今日头条2018校园招聘后端开发工程师(第四批)编程题 - 题解
- 用户即算法,知乎这次站在了今日头条的对面
- 3分钟了解今日头条推荐算法原理(附视频+PPT)
- Python 爬虫实例(3)—— 爬取今日头条as cp 算法 解密
- 12.20今日头条实习面试总结-算法工程师
- 阿里巴巴2018校园招聘运筹优化算法工程师编程题
- 今日头条败给了色情?AI算法不行,还是另有隐情?