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基础了解的学习记录

    一.文件结构图 linux的储存结构为文件树 二.绝对路径.相对路径.权限 1.绝对路径: /usr/local/include       在路径的最前面是 / 开头的 使用环境:当在当前路径下想到 ...

  2. centos7.3安装caffe出现错误:/bin/ld: cannot find -lcblas /bin/ld: cannot find -latlas

    安装caffe时需要依赖库atlas,可使用yum -y install atlas-devel  安装,但是安装之后还是有可能出现错误: /bin/ld: cannot find -lcblas / ...

  3. 【 LVS 】类型及算法

    一.概念: LVS( linux virtual server ) : Linux虚拟服务器 lvs是一个负载均衡设备,它不提供任何服务,用户请求到这里的时候,它将客户需求转发至后端的realserv ...

  4. python3的leetcode题,两个数求和等于目标值,返回这两个数的索引组成的列表(三种方法)

    给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为gai目标值的 两个 整数. 你可以假设每种输入只会对应一个答案.但是,你不能重复利用这个数组中同样的元素. 示例: 给定 ...

  5. 如何理解reliability

    首先推荐看: https://wenku.baidu.com/view/f55f400c52ea551810a68746.html 复习一下均值方差 然后重点看: https://www.social ...

  6. Workman websocket 握手连接

    默认的是TCP连接方式,如果需要WebSocket,则需要更改Gateway方式, 服务端协议要和客户端协议一致才能通讯.客户端是websocket协议,服务端也要设置成websocket协议.默认为 ...

  7. [thinkPHP] buildSql可以查看tp CURD操作对应的SQL

    $goods = M('Goods')->where($map)->buildSql(); echo $goods;

  8. 洛谷 P1068 分数线划定【结构体排序】

    题目描述 世博会志愿者的选拔工作正在 A 市如火如荼的进行.为了选拔最合适的人才,A 市对 所有报名的选手进行了笔试,笔试分数达到面试分数线的选手方可进入面试.面试分数线根 据计划录取人数的150%划 ...

  9. 树链剖分【p4114】Qtree-Query on a tree I

    Description 给定一棵n个节点的树,有两个操作: CHANGE i ti 把第i条边的边权变成ti QUERY a b 输出从a到b的路径中最大的边权,当a=b的时候,输出0 Input 第 ...

  10. ASP.NET Core 2.2 基础知识(十三) WebAPI 概述

    我们先创建一个 WebAPI 项目,看看官方给的模板到底有哪些东西 官方给出的模板: [Route("api/[controller]")] [ApiController] pub ...