Count Pairs

Description

You are given n circles centered on Y-aixs. The ith circle’s center is at point (i, 0) and its radius is A[i]. Count the number of pairs of circles that have at least one common point?

Input

The input should be a list of n positive integers A, each of them should be less than or equal to 1,000,000,000 and n should be less than or equal to 100,000.

Output

The output should be the number of pairs of circles that have at least one common point in the format of Integer.

Sample Input

1 2 3

Sample Output

3

图书馆排队问题。跟问题Number of Airplanes in the Sky差不多。将每个圆在x轴上的最左边的点与最右边的点放到vector中排序,同时标记是左边的点还是右边的点,注意如果多个点在同一个坐标,左边界的点要放在前面,然后一次遍历,遇到左边界就统计当前点在几个圆的范围内,即res += cnt,然后更新cnt,即++cnt,遇到右边界就--cnt。

 class Solution {
public:
long long countPairs(const vector<int> &A) {
long long res = , cnt = ;
vector<pair<int, int>> v;
for(int i = ; i < A.size(); ++i) {
v.push_back({i - A[i], -});
v.push_back({i + A[i], });
}
sort(v.begin(), v.end());
for (auto vv : v) {
if (vv.second == -) res += cnt++;
else --cnt;
}
return res;
}
};
/**************************************************************
Problem: 25
User: easonliu
Language: C++
Result: Accepted
Time:141 ms
Memory:8524 kb
****************************************************************/

[MeetCoder] Count Pairs的更多相关文章

  1. CodeForces - 1189E Count Pairs(平方差)

    Count Pairs You are given a prime number pp, nn integers a1,a2,…,ana1,a2,…,an, and an integer kk. Fi ...

  2. [CF1188B]Count Pairs 题解

    前言 这道题目是道好题. 第一次div-2进前100,我太弱了. 题解 公式推导 我们观察这个式子. \[(a_i+a_j)(a_i^2+a_j^2)\equiv k \mod p\] 感觉少了点什么 ...

  3. CodeForces - 1189 E.Count Pairs (数学)

    You are given a prime number pp, nn integers a1,a2,…,ana1,a2,…,an, and an integer kk. Find the numbe ...

  4. CF1188B/E Count Pairs(数学)

    数同余的个数显然是要把\(i,j\)分别放到\(\equiv\)的两边 $ (a_i + a_j)(a_i^2 + a_j^2) \equiv k \bmod p $ 左右两边乘上\((a_i-a_j ...

  5. CF1188B Count Pairs

    [题目描述] 给定一个质数 \(p\) , 一个长度为 \(n\)n 的序列 \(a = \{ a_1,a_2,\cdots,a_n\}\)一个整数 \(k\). 求所有数对 \((i, j)\) ( ...

  6. Codeforces 1189E. Count Pairs

    传送门 可以算是纯数学题了吧... 看到这个 $(x+y)(x^2+y^2)$ 就可以想到化简三角函数时经常用到的操作,左右同乘 那么 $(a_i+a_j)(a_i^2+a_j^2) \equiv  ...

  7. Codeforces 1188B Count Pairs (同余+分离变量)

    题意: 给一个3e5的数组,求(i,j)对数,使得$(a_i+a_j)(a_i^2+a_j^2)\equiv k\ mod\ p$ 思路: 化简$(a_i^4-a_j^4)\equiv k(a_i-a ...

  8. Codeforces 1188B - Count Pairs(思维题)

    Codeforces 题面传送门 & 洛谷题面传送门 虽说是一个 D1B,但还是想了我足足 20min,所以还是写篇题解罢( 首先注意到这个式子里涉及两个参数,如果我们选择固定一个并动态维护另 ...

  9. [Swift]LeetCode373. 查找和最小的K对数字 | Find K Pairs with Smallest Sums

    You are given two integer arrays nums1 and nums2 sorted in ascending order and an integer k. Define ...

随机推荐

  1. Hibernate学习笔记四:事务管理

    转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6768298.html  一:需要事务的Session操作 Session操作中,查询类操作是不需要事务就能生效 ...

  2. iOSUIWebView---快停下啦,你的愚蠢的行为

    公元前 之前还是学生时代的时候给社团们学弟学妹们介绍iOS编程的时候,简单的准备了图灵ios培训第一周(使用UIWebView创建简易浏览器), NSURL *url =[NSURL URLWithS ...

  3. having 与where 的异同点

    having 与where 的异同点: where针对表中的列发挥作用,查询数据 having对查询结果中的列发挥作用,筛选数据 #查询本店商品价格比市场价低多少钱,输出低200元以上的商品 ; // ...

  4. HDU 2217 Data Structure?

    C - Data Structure? Time Limit:5000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u ...

  5. 腾讯alloyteam团队前端代码规范

    来源于:http://alloyteam.github.io/CodeGuide/ 命名规则 项目命名 全部采用小写方式, 以下划线分隔. 例:my_project_name 目录命名 参照项目命名规 ...

  6. 用dockerfile构建基于centos系统的jar包的镜像

    实际示例: [root@master01 home-dataline]# ls dataline.jar Dockerfile jdk-8u181-linux-x64.tar.gz [root@mas ...

  7. iOS transform解决连续多次旋转缩放,实现图片旋转缩放效果

    一.需求 实现imageView的缩放旋转效果,一般有两种方式: 1.底层加scrollview,利用scrollview的属性实现.(推荐这种,这是我比较后发现的,手势做缩放旋转会有点弊端) 2.利 ...

  8. centos7用xshell可以连接, xftp连接失败!(墙裂推荐)

    环境: vultr vps搭建的是centos7  毛病: xshell可以连接, 打开xftp配置好连接却连接失败如下图: 然后开始设置ssh的配置文件vim /etc/ssh/sshd_confi ...

  9. javascript中的复制

    github 深度复制这个问题看似简单,实际上要想完美实现需要很多知识. 需要考虑Set,Map,Promise等类型的对象 这个库简洁明了,是一个小巧玲珑的JS库 Promise简单例子 funct ...

  10. keras callback中的stop_training

    keras这个框架简洁优美,设计上堪称典范.而tensorflow就显得臃肿庞杂,混乱不清.当然,keras的周边部件比如callbacks.datasets.preprocessing有许多过度设计 ...