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坦克大战 (二) 之画一个能动的圆圈代表坦克
本文来自:小易博客专栏.转载请注明出处:http://blog.csdn.net/oldinaction 在此小易将坦克大战这个项目分为几个版本,以此对J2SE的知识进行回顾和总结,希望这样也能给刚学 ...
- tcpip概述
网络协议通常分为不同层次进行开发,每一层分别负责不同的通信功能.一个类似TCPIP的协议簇是一组不同层次上的多个协议的组合.TCPIP通常被认为是一个四层协议系统,分为:应用层(telnet/FTP/ ...
- python基础复习-1-2 数据类型-str、list、tuple、dict
数据类型 数字 引号: 123 数值 '123' 字符串 整数:ini long 范围:(-2**31 - 2**31) num = 123 长整型 long (L) num = 123L 浮点型:f ...
- [ 总结 ] 删除通过find查找到的文件
[root@cloud abc]# touch test{,,,} [root@cloud abc]# ls shadow test test1 test2 test3 test5 [root@clo ...
- Image.FromStream与Image.FromFile使用区别
将一个图片加载并显示在picturebox上,一般情况下得到预期的结果,然而对于同一个filepath, 若连续两次调用下面的语句系统将会报错(如用户多次选择加载同一张图片使用Image.FromFi ...
- 微信支付http://www.cnblogs.com/True_to_me/p/3565039.html
公众号支付有2种支付方式: JS API 支付:是指用户打开图文消息或者扫描二维码,在微信内置浏览器打开网页进行的支付.商户网页前端通过使用微信提供的 JS API,调用微信支付模块.这种方式,适合需 ...
- src2中的alpha融合ROI
#include <cv.h> #include <highgui.h> int main(int argc, char** argv) { IplImage *src1,*s ...
- docker从零开始网络(四 ) host网络
使用主机网络 如果host对容器使用网络驱动程序,则该容器的网络堆栈不会与Docker主机隔离.例如,如果您运行绑定到端口80 host的容器并使用网络,则容器的应用程序将在主机IP地址的端口80上可 ...
- Zookeeper概念学习系列之zookeeper实现分布式共享锁
首先假设有两个线程, 两个线程要同时到mysql中更新一条数据, 对数据库中的数据进行累加更新.由于在分布式环境下, 这两个线程可能存在于不同的机器上的不同jvm进程中, 所以这两个线程的关系就是垮主 ...
- 极光推送配置(Android Studio),亲测有效
进行到这里就可以接收到通知了,但是如果你还想根据接收的消息做点什么 step8: public class MyReceiver extends BroadcastReceiver { private ...