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和Js的高精度计算
转载自:http://blog.csdn.net/zhutulang/article/details/6844834#comments Java: import java.math.BigDecima ...
- [ Openstack ] Openstack-Mitaka 高可用之 Pacemaker+corosync+pcs 高可用集群
目录 Openstack-Mitaka 高可用之 概述 Openstack-Mitaka 高可用之 环境初始化 Openstack-Mitaka 高可用之 Mariadb-Galera集群 ...
- [ Python - 9 ] 高阶函数map和reduce连用实例
1. 利用map和reduce编写一个str2float函数,把字符串'123.456'转换成浮点数123.456: from functools import reduce def str2num( ...
- win 7 浏览器被篡改小插曲
今天下班回家,打开台式机发现IE,火狐都被篡改了.作为运维都会有点强迫症.这是个桌面系统,实在是没兴趣捣鼓.但是还是没办法,经常要用.等我下次有空了,直接换linux好了. 于是开始排查问题吧: 1. ...
- 定义表单控件的id和name注意点
最近在学习JavaScript,在编写一个demo时出现一个错误.为表单中的提交按钮控件定义的id属性值为submit,致使程序出错.如下代码:(js代码省略) <form method=&qu ...
- KO工作原理及带来的好处
介绍 Knockout是一个以数据模型(data model)为基础的能够帮助你创建富文本,响应显示和编辑用户界面的JavaScript类库.任何时候如果你的UI需要自动更新(比如:更新依赖于用户的行 ...
- Nessus home与Nexpose community 对比
转载请注明来源:http://www.cnblogs.com/phoenix--/p/3345569.html 更新:Nessus home版限制了,总量:16,Nexpose限制了总量为32,全部没 ...
- 最快的序列化组件protobuf的.net版本protobuf.net
Protobuf是google开源的一个项目,用户数据序列化反序列化,google声称google的数据通信都是用该序列化方法.它比xml格式要少的多,甚至比二进制数据格式也小的多. Prot ...
- 【python】ipython与python的区别
[python]ipython与python的区别 (2014-06-05 12:27:40) 转载▼ 分类: Python http://mba.shengwushibie.com/itbook ...
- 51nod 1459 迷宫游戏【最短路拓展】
1459 迷宫游戏 基准时间限制:1 秒 空间限制:131072 KB 你来到一个迷宫前.该迷宫由若干个房间组成,每个房间都有一个得分,第一次进入这个房间,你就可以得到这个分数.还有若干双向道路连 ...