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. mint 设置无线 AP

    所需软件: sudo apt-get install hostapd 1. 创建 hostapd 的 configure 文件 新建 hostapd.conf 文件,存放位置任意,与后面修改的路径一致 ...

  2. Oracle官方文档

    Oracle DBA 10g 两日速成课程 http://www.oracle.com/webfolder/technetwork/cn/tutorials/obe/db/10g/r2/2day_db ...

  3. AJAX的同步返回结果值

    function makeJQGridDataFromList(url) {     var rowData;     var viewPage = 0;     var viewTotal = 0; ...

  4. mybatis generator(MyBatis的逆向工程)

    1创建数据表 如图所示:我的是在text数据库中创建了一个Student表,字段有id(int),   name(varchar),     age(int),    score(int) 2创建项目 ...

  5. 基于springboot跟poi封装的最便捷的excel导出

    发布时间:2018-11-15   技术:springboot1.5.6 + maven3.0.5 + jdk1.8   概述 Springboot最便捷的Excel导出,只需要一个配置文件即可搞定 ...

  6. jdbc连接Oracle连接字符串方法

  7. 用yum下载rpm包(不安装)到制定目录

    用yum下载rpm包(不安装)到制定目录用yum下载rpm包 www.pcjsh.com  1.安装yum-downloadonly # yum install yum-downloadonly -y ...

  8. Tensorflow结点打包和依赖控制

    深度学习库能够充分发挥GPU并行计算的能力,但是有时我们却不得不需要串行.这时就需要用到依赖控制. import tensorflow as tf a = tf.Variable(1) b = tf. ...

  9. C++11开发中的Atomic原子操作

    C++11开发中的Atomic原子操作 Nicol的博客铭 原文  https://taozj.org/2016/09/C-11%E5%BC%80%E5%8F%91%E4%B8%AD%E7%9A%84 ...

  10. 自动化部署必备技能—搭建YUM仓库

    导言: YUM主要用于自动安装.升级rpm软件包,它能自动查找并解决rpm包之间的依赖关系.要成功的使用YUM工具安装更新软件或系统,就需要有一个包含各种rpm软件包的repository(软件仓库) ...