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. JqGrid把数据行插入指定位置的方法addRowData

    1.首页在colModel里写好方法,如下代码options.rowId是获取当前行的编号 { label: '操作', width: 60, align: 'center', formatter: ...

  2. Oracle列自增实现(3)-DEFAULT Values Using Sequences

    Oracle 12c中,可以使用序列的NEXTVAL and CURRVAL的值作为默认值,来实现列自增! 一.使用序列的NEXTVAL and CURRVAL的值作为默认值 创建序列 CREATE ...

  3. oracle 清空表数据的2种方式及速度比较

            1.情景展示 现在,需要清空该表数据 2.实现方式 为了比较删除速度,对该表进行复制 确认复制的表和原来的表数据是否一致 方式一:使用truncate table实现 方式二:使用de ...

  4. INT函数和ROUND

    语法:INT(number) Number 需要进行向下舍入取整的实数

  5. mingw 构建 Geos

    简述 在做某个小程序时候用到了QT,而用的Qt是mingw版本的,所以使用mingw构建了一下geos库. 1.准备工作 首先需要先安装好mingw,这里直接使用http://www.mingw-w6 ...

  6. onActivityResult 传递数据

    onActivityResult 传递数据 http://www.cnblogs.com/sipher/articles/2435078.html 如下图所示.当菜单项变多时,出现了垂直的滚动条,选项 ...

  7. android中执行(定时任务)的方法及6位随机码的产生

    在网上看了很多类似的文章,比较乱,自己总结了一下,在开发中,常见的执行定时任务的方法有以下几种, 很简单的描述,有什么不懂可以留言,下面来介绍一下这几种常见的方法: 1.直接在线程中睡觉的方法,这个比 ...

  8. ios中常用的方法

    图片分类 @implementation UIImageView (ext) +(UIImageView*)imageViewWith:(UIImage*)img imgFrame:(CGRect)_ ...

  9. db_table--Spring Security3.1 最新配置实例

    2011-04-28 这几天学习了一下Spring Security3.1,从官网下载了Spring Security3.1版本进行练习,经过多次尝试才摸清了其中的一些原理.本人不才,希望能帮助大家. ...

  10. blktrace + blkparse + btt 分析IO

    blktrace是一款block层的trace工具,block层在IO路径上的位置: 一个IO的生命周期大约是: ● I/O enters block layer – it can be: – Rem ...