链接:https://codeforces.com/contest/1166/problem/C

题意:

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 nnintegers 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 yywere equal to these two values. Note that it is possible that Jose is wrong, and that no pairs could possibly make the legend true.

思路:

因为需要满足条件 min(|x-y|, |x+y|) <= |x| <= |y| <= max(|x-y|, |x+y|).

所以将x变成-x并不影响条件。所以将所有输入全部取绝对值。

再排序,二分查找。

代码:

#include <bits/stdc++.h>
using namespace std; typedef long long LL;
const int MAXN = 2e5+10; int a[MAXN]; int main()
{
int n;
cin >> n;
for (int i = 1;i <= n;i++)
cin >> a[i], a[i] = abs(a[i]);
sort(a+1, a+1+n);
LL res = 0;
for (int i = 1;i <= n;i++)
res += (upper_bound(a+1, a+1+n, 2*a[i])-a)-i-1;
cout << res << endl; return 0;
}

  

最新文章

  1. 【Python基础学习六】函数
  2. Linux上设置memcached自启动
  3. linux -- nano
  4. linux病毒
  5. DEV控件GridControl常用属性设置
  6. Apache Spark源码走读之23 -- Spark MLLib中拟牛顿法L-BFGS的源码实现
  7. 在腾讯云上创建您的SQL Cluster(1)
  8. Python连接Redis连接配置
  9. Android 判断数据库中是否存在某个表
  10. -_-#【Mac】命令
  11. cocos2d-x 3.0游戏实例学习笔记 《跑酷》第四步--地图循环&amp;amp;主角加入动作
  12. Word字体与像素的对应关系(转)
  13. zabbix_server表面启动成功,但是没有进程
  14. java数据结构与算法(一)
  15. laravel 5.5 接入蚂蚁金服官方SDK(支付宝APP支付为例)开发步骤
  16. Helm学习笔记
  17. 洛谷p3801:红色的幻想乡
  18. oracle 安装提示未找到文件安装
  19. vs2017 .net core 项目调试浏览器网页闪退Bug
  20. Linux文件权限设置

热门文章

  1. 大数据- 自定义Log4j日记
  2. jQuery+CSS3实现弯曲文字路径
  3. laravel基础课程---12、lavarel的ajax操作2(lavarel的ajax使用总结)
  4. 使用谷歌浏览器进行Web开发技巧
  5. C/C++协程的实现方式总结
  6. tf.stack和tf.unstack
  7. codevs 1144 守望者的逃离
  8. poj1191棋盘分割——区间DP
  9. 分布式一致性协议之:Paxos算法(转)
  10. Android应用如何反馈Crash报告