[MeetCoder] 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的更多相关文章
- 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 ...
- [CF1188B]Count Pairs 题解
前言 这道题目是道好题. 第一次div-2进前100,我太弱了. 题解 公式推导 我们观察这个式子. \[(a_i+a_j)(a_i^2+a_j^2)\equiv k \mod p\] 感觉少了点什么 ...
- 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 ...
- 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 ...
- CF1188B Count Pairs
[题目描述] 给定一个质数 \(p\) , 一个长度为 \(n\)n 的序列 \(a = \{ a_1,a_2,\cdots,a_n\}\)一个整数 \(k\). 求所有数对 \((i, j)\) ( ...
- Codeforces 1189E. Count Pairs
传送门 可以算是纯数学题了吧... 看到这个 $(x+y)(x^2+y^2)$ 就可以想到化简三角函数时经常用到的操作,左右同乘 那么 $(a_i+a_j)(a_i^2+a_j^2) \equiv ...
- 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 ...
- Codeforces 1188B - Count Pairs(思维题)
Codeforces 题面传送门 & 洛谷题面传送门 虽说是一个 D1B,但还是想了我足足 20min,所以还是写篇题解罢( 首先注意到这个式子里涉及两个参数,如果我们选择固定一个并动态维护另 ...
- [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 ...
随机推荐
- Xamarin.Android服务的实现
一.服务的生命周期 服务与活动一样,在它的整个生命周期中存在着一些事件,下图可以很好解释整个过程以及涉及到的方法: 在真实的使用中,Service来还包含一个OnBind方法,并且必须要使用该方法,但 ...
- 使用OpenNI 2获取RGBD摄像头深度信息
NiViewer 安装好摄像头驱动和OpenNI后,在Tools文件夹中可以找到一个程序NiViewer.NiViewer的一些基本控制方法如下: 1. ESC关闭NiViewer程序 2. 右键可以 ...
- Vivaldi浏览器媲美Chrome
Vivaldi跨平台的浏览器,很好的兼容性...基本上跟Chrome一个层次的... 好的东西,用一次就明白!好酒,酒香巷子深... Download: https://vivaldi.com/dow ...
- QTableView中使用Delegate方式来实现对特定列的文本进行换行
问题:由于表格的一个列中,有个别文本过长,默认情况下,QTableView不支持对某列的文本换行,所以需要通过Delegate方式来实现这样的效果 头文件的Delegate #pragma once ...
- Linux十大常用命令
No 命令 功能 实例 解释 mkdir 创建目录 mkdir newdirmkdir dir/subdirmkdir -p newdir/subdir -p: 可以是一个路径名称.此时若路径中的某些 ...
- PendingIntent传递数据注意参数RequestCode和Flag
数据发送方: public static void notify(Context context, TxrjMessage msg) { NotificationManager notifiM ...
- java实现读取ftp服务器上的csv文件
定义ftp操作接口 import java.io.InputStream; import java.util.List; import org.apache.commons.net.ftp.FTPCl ...
- java mongodb 基础系列---查询,排序,limit,$in,$or,输出为list,创建索引,$ne 非操作
官方api教程:http://docs.mongodb.org/ecosystem/tutorial/getting-started-with-java-driver/#getting-started ...
- 【LeetCode】214. Shortest Palindrome
Shortest Palindrome Given a string S, you are allowed to convert it to a palindrome by adding charac ...
- 利用Apache配置http expires值提高网站性能
HTTP头中有个expires参数,设置一个未来的时间,在这时间以前,浏览器会先从cache读取,如果没有再从服务器中读取.对于像图片,css,script等静态内容,只需发一次http reques ...