Solution of NumberOfDiscIntersections by Codility
question:https://codility.com/programmers/lessons/4
this question is seem like line intersections question. we can use similar method to solve this question.
now i prove this solution. for each line, it has two endpoint. we call it left and right. assume that the left and right have been sorted .
if line i and line j intersection, we can conclude that left[i]<right[j] or left[j]<right[i]. ... how to say?
?
trap: int overflow
code:
#include <algorithm>
int solution(vector<int> &A) {
// write your code in C++11
int size = A.size();
if (size <2)
return 0;
vector<long> begin;
vector<long> end;
for(int i=0; i<size; i++){
begin.push_back(i-static_cast<long>(A[i]));
end.push_back(i+static_cast<long>(A[i]));
}
sort(begin.begin(),begin.end());
sort(end.begin(),end.end());
int res = 0;
int upper_index =0, lower_index=0;
for(upper_index =0; upper_index<size; upper_index++){
while(lower_index<size && begin[lower_index]<= end[upper_index])
lower_index++;
res += lower_index-upper_index-1;
if (res >10000000)
return -1;
}
return res;
}
Solution of NumberOfDiscIntersections by Codility的更多相关文章
- Solution to Triangle by Codility
question: https://codility.com/programmers/lessons/4 we need two parts to prove our solution. on one ...
- the solution of CountNonDivisible by Codility
question:https://codility.com/programmers/lessons/9 To solve this question , I get each element's di ...
- 做了codility网站上一题:CountBoundedSlices
在写上一随笔之前,在Codility网站上还做了一个道题(非Demo题):CountBoundedSlices,得了60分(害臊呀).今天又重新做了一下这个算法,性能提高了不少,但由于此题不是Demo ...
- Codility NumberSolitaire Solution
1.题目: A game for one player is played on a board consisting of N consecutive squares, numbered from ...
- codility flags solution
How to solve this HARD issue 1. Problem: A non-empty zero-indexed array A consisting of N integers i ...
- *[codility]Number-of-disc-intersections
http://codility.com/demo/take-sample-test/beta2010/ 这题以前做的时候是先排序再二分,现在觉得没有必要.首先圆可以看成线段,把线段的进入作为一个事件, ...
- GenomicRangeQuery /codility/ preFix sums
首先上题目: A DNA sequence can be represented as a string consisting of the letters A, C, G and T, which ...
- *[codility]Peaks
https://codility.com/demo/take-sample-test/peaks http://blog.csdn.net/caopengcs/article/details/1749 ...
- *[codility]Country network
https://codility.com/programmers/challenges/fluorum2014 http://www.51nod.com/onlineJudge/questionCod ...
随机推荐
- Java易错知识点(2) - 在读取Cookie时除了Key,Value是得不到其他信息的
全文总结: 在读取Cookie,然后操作时,除了getName(),getValue()外,不要妄图得到其他信息,如下方法不会得到值的: cookie.getMaxAge(); cookie.getD ...
- 工作中常用Linux命令--服务器运维
工作中常用Linux命令--服务器运维 lsof查看端口使用情况 lsof -i:8080更多lsof命令使用说明:http://www.cnblogs.com/peida/archive/2013/ ...
- React.js入门
React 入门实例教程 现在最热门的前端框架,毫无疑问是 React . 上周,基于 React 的 React Native 发布,结果一天之内,就获得了 5000 颗星,受瞩目程度可见一斑. ...
- GIT指令简洁篇
查看.添加.提交.删除.找回,重置修改文件 git help <command> # 显示command的help git show # 显示某次提交的内容 git show $id gi ...
- JMeter 分布式测试部署
对于并发量很大的需求,如上万并发量,受到CPU和内存的限制,单机模拟场景是实现不了的,为了让JMeter提供更大的负载能力,须使用它的分布式机制,即多台机器同时产生负载的功能. 以下参数分析可用于配置 ...
- 【C++】指针和new相关
看黄邦勇帅的笔记. 指针和new之前觉得已经掌握的很好了,可是看了资料还是get到了新知识.记录一下. 1.指针只支持 4 种算术运算符:++,――,+,-.指针只能与整数加减.指针运算的原则是:每当 ...
- iOS运行时,如何增加成员变量
必须实现getter和setter方法 ``` - (void)setName:(NSString *)name { /** * 为某个类关联某个对象 * * @param object#> 要 ...
- POJ 2503.Babelfish-sscanf()函数+strcmp()函数+二分
Babelfish Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 44545 Accepted: 18803 Des ...
- Codeforces 1039A. Timetable
题目地址:http://codeforces.com/problemset/problem/1039/A 题目的关键在于理清楚思路,然后代码就比较容易写了 对于每一个位置的bus,即对于每一个i(i& ...
- 2、Flask实战第2天:URL传参
当我们访问网站/的时候,会执行hell_world函数,并把这个函数的返回值返回给浏览器,这样浏览器就显示hello world了 @app.route('/') def hello_world(): ...