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的更多相关文章

  1. Solution to Triangle by Codility

    question: https://codility.com/programmers/lessons/4 we need two parts to prove our solution. on one ...

  2. the solution of CountNonDivisible by Codility

    question:https://codility.com/programmers/lessons/9 To solve this question , I get each element's di ...

  3. 做了codility网站上一题:CountBoundedSlices

    在写上一随笔之前,在Codility网站上还做了一个道题(非Demo题):CountBoundedSlices,得了60分(害臊呀).今天又重新做了一下这个算法,性能提高了不少,但由于此题不是Demo ...

  4. Codility NumberSolitaire Solution

    1.题目: A game for one player is played on a board consisting of N consecutive squares, numbered from ...

  5. codility flags solution

    How to solve this HARD issue 1. Problem: A non-empty zero-indexed array A consisting of N integers i ...

  6. *[codility]Number-of-disc-intersections

    http://codility.com/demo/take-sample-test/beta2010/ 这题以前做的时候是先排序再二分,现在觉得没有必要.首先圆可以看成线段,把线段的进入作为一个事件, ...

  7. GenomicRangeQuery /codility/ preFix sums

    首先上题目: A DNA sequence can be represented as a string consisting of the letters A, C, G and T, which ...

  8. *[codility]Peaks

    https://codility.com/demo/take-sample-test/peaks http://blog.csdn.net/caopengcs/article/details/1749 ...

  9. *[codility]Country network

    https://codility.com/programmers/challenges/fluorum2014 http://www.51nod.com/onlineJudge/questionCod ...

随机推荐

  1. [ 总结 ] Linux系统测试硬盘I/O

    检测硬盘I/O相对来说还是一个比较抽象的概念,但是对系统性能的影响还是至关重要的. (1)使用hdparm命令检测读取速度:    hdparm命令提供了一个命令行的接口用于读取和设置IDE和SCSI ...

  2. getResourceAsStream用法详解

    //使用绝对路径,否则无法读取config.properties //InputStream inStream=new FileInputStream("F:\\android\\test\ ...

  3. 经常用到的Eclipse快捷键(更新中....)

    alt+shift+s:弹出Source选项,用于生成get,set等方法. Ctrl+E:快速显示当前Editer的下拉列表 alt+shift+r:重命名 Ctrl+Shift+→/Ctrl+Sh ...

  4. MBProgressHUD自定义视图大小的修改

    MBProgressHUD 一款简单易用的弹窗,但是在使用中难免使用自定义view即customView,此时会发现HUD的弹窗大小和你image的大小是一样的无论你怎么修改frame也没有用,此时你 ...

  5. CSS3实现鼠标经过图片时图片放大

    在鼠标经过图片时,图片被放大,而且还有个过渡的效果.... <!DOCTYPE html> <html> <head> <link href="cs ...

  6. [xampp] phpmyadmin 设置登录密码

    $ cd /opt/lampp/bin $ ./mysqladmin -u root password 'new_password' $ vim ../phpmyadmin/config.inc.ph ...

  7. 安装mezzanine时报:storing debug log for failure【已解决】

    同时还提示: bz2 module is not found(貌似) 解决方法: 1.重新安装python wget http://bzip.org/1.0.6/bzip2-1.0.6.tar.gz ...

  8. 福州三中集训day3

    Day3数据结构,强无敌. 基本讲的是栈,队列,链表,都是些还会的操作,然后接着讲的就比较心凉凉了,先讲了堆,然后是hsah 栈,队列,链表问题都不大,笔记记得都还好,堆就凉凉了. 不会不会不会,没学 ...

  9. hdu6230

    hdu6230 题意 给出一个字符串,问有多少个子串 \(S[1..3n-2](n \geq 2)\) 满足 \(S[i]=S[2n-i]=S[2n+i-2] (1\leq i \leq n)\) . ...

  10. centos7下自定义服务启动和自动执行脚本

    systemctl list-units --type=service #查看所有已启动的服务 systemctl enable httpd.service #加入开机自启动服务 systemctl ...