Codeforces Round #561 (Div. 2) A Tale of Two Lands 【二分】
A Tale of Two Lands
The legend of the foundation of Vectorland talks of two integers xx and yy. Centuries ago, the array king placed two markers at points |x||x| and |y||y| on the number line and conquered all the land in between (including the endpoints), which he declared to be Arrayland. Many years later, the vector king placed markers at points |x−y||x−y| and |x+y||x+y| and conquered all the land in between (including the endpoints), which he declared to be Vectorland. He did so in such a way that the land of Arrayland was completely inside (including the endpoints) the land of Vectorland.
Here |z||z| denotes the absolute value of zz.
Now, Jose is stuck on a question of his history exam: "What are the values of xx and yy?" Jose doesn't know the answer, but he believes he has narrowed the possible answers down to nn integers a1,a2,…,ana1,a2,…,an. Now, he wants to know the number of unordered pairs formed by two different elements from these nn integers such that the legend could be true if xx and yy were equal to these two values. Note that it is possible that Jose is wrong, and that no pairs could possibly make the legend true.
Input
The first line contains a single integer nn (2≤n≤2⋅1052≤n≤2⋅105) — the number of choices.
The second line contains nn pairwise distinct integers a1,a2,…,ana1,a2,…,an (−109≤ai≤109−109≤ai≤109) — the choices Jose is considering.
Output
Print a single integer number — the number of unordered pairs {x,y}{x,y} formed by different numbers from Jose's choices that could make the legend true.
Examples
Input
3
2 5 -3
Output
2
Input
2
3 6
Output
1
Note
Consider the first sample. For the pair {2,5}{2,5}, the situation looks as follows, with the Arrayland markers at |2|=2|2|=2 and |5|=5|5|=5, while the Vectorland markers are located at |2−5|=3|2−5|=3 and |2+5|=7|2+5|=7:
The legend is not true in this case, because the interval [2,3][2,3] is not conquered by Vectorland. For the pair {5,−3}{5,−3} the situation looks as follows, with Arrayland consisting of the interval [3,5][3,5] and Vectorland consisting of the interval [2,8][2,8]:
As Vectorland completely contains Arrayland, the legend is true. It can also be shown that the legend is true for the pair {2,−3}{2,−3}, for a total of two pairs.
In the second sample, the only pair is {3,6}{3,6}, and the situation looks as follows:
Note that even though Arrayland and Vectorland share 33 as endpoint, we still consider Arrayland to be completely inside of Vectorland.
题意:
给出一些坐标(均在x轴上) 从中任意选取两个点a、b 要求满足: 区间 [a,b]在区间 [abs(a+b) , abs(a-b)]内 输出能够组成的个数
思路:
(1)无论点的坐标值是正或负与结果都无关 所以把所有数转换成正数存进去
(2)把所有坐标值排序
(3)假设两个坐标值a<b 若满足2*a>=b 那么就符合题意要求 并且a和b之间的任意一个坐标值与b组合都符合要求
注意:
将坐标值变成绝对值后 可能出现坐标值相等的情况 但也要把它们看成两种情况 因为输入的时候是一正一负 所以两次都要算
AC代码:
二分模板题,但写二分的时候没注意用ans标记前一个mid值 导致错误
#include<bits/stdc++.h>
using namespace std;
typedef long long int LL;
const int MAX=2e5;
int a[MAX+5],n;
LL sum;
int main()
{
scanf("%d",&n);
for(int i=0;i<n;i++){
scanf("%d",&a[i]);
a[i]=abs(a[i]);
}
sort(a,a+n);
for(int i=0;i<n;i++){
int l=0,r=i,ans=-1;
while(l<=r){
int mid=(l+r)/2;
if(a[mid]*2>=a[i]){
r=mid-1;
ans=mid;
}
else{
l=mid+1;
}
}
if(ans!=-1){
sum+=(i-ans);
}
}
printf("%lld\n",sum);
return 0;
}
Codeforces Round #561 (Div. 2) A Tale of Two Lands 【二分】的更多相关文章
- Codeforces Round #561 (Div. 2)
C. A Tale of Two Lands 题意: 给出 n 个数,问有多少点对(x,y)满足 |x-y| ≤ |x|,|y| ≤ |x+y|: (x,y) 和 (y,x) 表示一种答案: 题解: ...
- Codeforces Round #561 (Div. 2) C. A Tale of Two Lands
链接:https://codeforces.com/contest/1166/problem/C 题意: The legend of the foundation of Vectorland talk ...
- Codeforces Round #561 (Div. 2) B. All the Vowels Please
链接:https://codeforces.com/contest/1166/problem/B 题意: Tom loves vowels, and he likes long words with ...
- Codeforces Round #561 (Div. 2) A. Silent Classroom
链接:https://codeforces.com/contest/1166/problem/A 题意: There are nn students in the first grade of Nlo ...
- Codeforces Round 561(Div 2)题解
这是一场失败的比赛. 前三题应该是随便搞的. D有点想法,一直死磕D,一直WA.(赛后发现少减了个1……) 看E那么多人过了,猜了个结论交了真过了. 感觉这次升的不光彩……还是等GR3掉了洗掉这次把, ...
- Codeforces Round #561 (Div. 2) E. The LCMs Must be Large(数学)
传送门 题意: 有 n 个商店,第 i 个商店出售正整数 ai: Dora 买了 m 天的东西,第 i 天去了 si 个不同的个商店购买了 si 个数: Dora 的对手 Swiper 在第 i 天去 ...
- Codeforces Round #561 (Div. 2) A. Silent Classroom(贪心)
A. Silent Classroom time limit per test1 second memory limit per test256 megabytes inputstandard inp ...
- Codeforces Round #307 (Div. 2) C. GukiZ hates Boxes 贪心/二分
C. GukiZ hates Boxes Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/551/ ...
- Codeforces Round #280 (Div. 2) D. Vanya and Computer Game 二分
D. Vanya and Computer Game Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contes ...
随机推荐
- oracle [精华] 你是否仍迷信rowid分页?
http://www.itpub.net/thread-1603830-1-1.html
- 常用的code snipper
Xcode 10.2.1 版本,自动代码提示功能延迟很大,很多情况下代码提示甚至会无效,但是code snippet 唤起的响应很快,从提高效率的情况考虑,即使系统提供的代码提示制作成code sni ...
- C#不定长参数
Test(5,6,7,8,9,10); void Test(int sd, params int[] arr)//不定长参数前要加params { for(int i = 0 ; i < arr ...
- 上位机开发之三菱FX3U以太网通信实践
上次跟大家介绍了一下上位机与三菱Q系列PLC通信的案例,大家可以通过点击这篇文章:上位机开发之三菱Q系列PLC通信实践(←戳这里) 今天以三菱FX3U PLC为例,跟大家介绍一下,如何实现上位机与其之 ...
- SPA页面
背景: 单页 Web 应用 (single-page application 简称为 SPA) 是一种特殊的 Web 应用,它将所有的活动均局限于一个Web页面中:这就表示Web应用被加载出来之后,W ...
- Docker 容器优雅终止方案
原文链接:Docker 容器优雅终止方案 作为一名系统重启工程师(SRE),你可能经常需要重启容器,毕竟 Kubernetes 的优势就是快速弹性伸缩和故障恢复,遇到问题先重启容器再说,几秒钟即可恢复 ...
- 【Hadoop高级】Hadoop HA、hdfs安全模式
Hadoop HA Safemode(安全模式) During start up the NameNode loads the file system state from the fsimage a ...
- (Java实现) 最佳调度问题
题目描述 假设有n个任务由k个可并行工作的机器完成.完成任务i需要的时间为ti.试设计一个算法找出完成这n个任务的最佳调度,使得完成全部任务的时间最早. 对任意给定的整数n和k,以及完成任务i需要的时 ...
- Java实现 LeetCode 488 祖玛游戏
488. 祖玛游戏 回忆一下祖玛游戏.现在桌上有一串球,颜色有红色,黄色(Y),蓝色(B),绿色(G),还有白色(W). 现在你手里也有几个球. 每一次,你可以从手里的球选一个,然后把这个球插入到一串 ...
- Java实现 LeetCode 273 整数转换英文表示
273. 整数转换英文表示 将非负整数转换为其对应的英文表示.可以保证给定输入小于 231 - 1 . 示例 1: 输入: 123 输出: "One Hundred Twenty Three ...