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. Centos7/RHEL 7 配置静态路由

    如图: 业务地址:192.168.10.0/24    192.168.20.0/24管理地址:172.168.10.0/24    172.168.20.0/24 需求:每台主机配置两张网卡,分别连 ...

  2. 【 Keepalived 】Nginx or Http 主-主模式

    上一篇:[ Keepalived ]Nginx or Http 主-备模式 在此基础上进行修改并实现 Keepalived主-主模式 首先,需要理解的是:主-备模式是一个VIP在工作,主-主模式则需要 ...

  3. 【计算机网络】http状态码

    100-199 用于指定客户端应相应的某些动作. 200-299 用于表示请求成功. 300-399 用于已经移动的文件并且常被包含在定位头信息中指定新的地址信息. 400-499 用于指出客户端的错 ...

  4. DELPHI数组,指针,字符串转换的例子

    关于数组,指针,字符串转换的例子 var   aa:   array [0..5] of Char;   bb:Pointer;   cc:string;   dd:PChar; procedure ...

  5. ORM-学生信息系统

    学生信息管理 展示学生信息 URL部分 url(r'^student_list/', app01_views.student_list, name="student_list"), ...

  6. shell字符串变量的特异功能:字符串的替换(${str/源模式/目标模式},${str//源模式/目标模式})、截断

    https://blog.csdn.net/wzb56_earl/article/details/6953612

  7. [BZOJ3027][Ceoi2004]Sweet 容斥+组合数

    3027: [Ceoi2004]Sweet Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 135  Solved: 66[Submit][Status] ...

  8. EF中使用Linq时First、FirstOrDefault、Single、SingleOrDefault几个方法的区别

    在使用EntityFramework开发时,.NET的System.Linq.Enumerable类为我们提供了许多Linq方法. 给大家分享一下关于First.FirstOrDefault.Sing ...

  9. python基础-匿名函数、内置函数、正则表达式、模块

    1. 匿名函数 1.1 有名函数 有名函数:定义了一个函数名,函数名指向内存地址:通过函数名进行访问.函数名加括号就可以运行有名函数,例如:func() def func(x, y, z = 1): ...

  10. python基础-协程函数、递归、模块、包等内容

    1. 协程函数 1.1 yield基本用法 yield作用 1.把函数的执行结果封装好,即封装__iter__和__next__,即得到一个迭代器 2.与return功能类似,都可以返回值,但不同的是 ...