1324D - Pair of Topics[二分]

time limit per test memory limit per test input output
2 seconds 256 megabytes standard input standard output

Description:

The next lecture in a high school requires two topics to be discussed. The i i i-th topic is interesting by a i a_i ai​ units for the teacher and by b i b_i bi​ units for the students.
The pair of topics i i i and j ( i < j ) j (i<j) j(i<j) is called good if a i + a j > b i + b j a_i+a_j>b_i+b_j ai​+aj​>bi​+bj​ (i.e. it is more interesting for the teacher).
Your task is to find the number of good pairs of topics.

Input

The first line of the input contains one integer n n n ( 2 ≤ n ≤ 2 ⋅ 1 0 5 ) (2≤n≤2⋅10^5) (2≤n≤2⋅105) — the number of topics.
The second line of the input contains n n n integers a 1 , a 2 , … , a n ( 1 ≤ a i ≤ 1 0 9 ) a_1,a_2,…,a_n (1≤a_i≤10^9) a1​,a2​,…,an​(1≤ai​≤109), where a i a_i ai​ is the interestingness of the i i i-th topic for the teacher.
The third line of the input contains n n n integers b 1 , b 2 , … , b n ( 1 ≤ b i ≤ 1 0 9 ) b_1,b_2,…,b_n (1≤b_i≤10^9) b1​,b2​,…,bn​(1≤bi​≤109), where b i b_i bi​ is the interestingness of the i i i-th topic for the students.

Output

Print one integer — the number of good pairs of topic.


One Example input

5
4 8 2 6 2
4 5 4 1 3

One Example output

7

Two Example input

4
1 3 2 4
1 3 2 4

Two Example output

0


分析:
题意:
求 a i + a j > b i + b j ( i < j ) a_i + a_j > b_i + b_j(i < j) ai​+aj​>bi​+bj​(i<j)的对数
做法:
其实就是求 a i − b i > − ( a j − b j ) ( i < j ) a_i - b_i > -(a_j - b_j)(i < j) ai​−bi​>−(aj​−bj​)(i<j)的对数
即 − ( a i + b i ) < a j + b j ( i < j ) -(a_i + b_i) < a_j + b_j (i < j) −(ai​+bi​)<aj​+bj​(i<j)的对数
现在令 c i = a i + b i c_i = a_i + b_i ci​=ai​+bi​(方便写)
仔细想一下,如果现在有一个 − c i > c j ( i > j ) -c_i > c_j(i > j) −ci​>cj​(i>j)其实这个是不符合的
但是将 i , j i, j i,j换一下,其实也就是我们要求的 − c j > c i ( j < i ) -c_j > c_i(j < i) −cj​>ci​(j<i)
所以其实跟位置并没有什么关系
只要排序一下每次找到大于 − c i -c_i −ci​的第一个数的位置就可以了

Code:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 2e5 + 5;int a[maxn];
int b[maxn];int main() {int n;scanf("%d", &n);for(int i = 1; i <= n; ++i)scanf("%d", &a[i]);for(int i = 1; i <= n; ++i)scanf("%d", &b[i]);for(int i = 1; i <= n; ++i)a[i] -= b[i];ll ans = 0;sort(a + 1, a + 1 + n);for(int i = 1; i <= n; ++i) {int x = upper_bound(a + i + 1, a + 1 + n, - a[i]) - a;ans += (n - x + 1) * 1ll;}printf("%lld\n", ans);return 0;
}

[Codeforces Round #627]1324D - Pair of Topics[二分]相关推荐

  1. [codeforces 1324D] Pair of Topics 分而治之+排列组合

    Codeforces Round #627 (Div. 3)   比赛人数6434 [codeforces 1324C]  Frog Jumps   一直向右+边界处理 总目录详见https://bl ...

  2. CodeForces - 1324D Pair of Topics (分治+排序)

    CodeForces - 1324D Pair of Topics 题目大意: 这题大意ai+aj>bi+bj全在这个式子上,就问你满足的组合有几种, 题目分析: 整理一下,得到(ai-bi)+ ...

  3. Codeforces Round #627 (Div. 3) E. Sleeping Schedule dp

    传送门 文章目录 题意: 思路: 题意: 给你一天hhh小时,初始时间是000,每天可以使时间+ai+a_i+ai​或者+ai−1+a_i-1+ai​−1,问最多可以让多少天的时间在[l,r][l,r ...

  4. CodeForces - 1324D Pair of Topics(思维+二分)

    题目链接:https://vjudge.net/contest/362265#problem/D The next lecture in a high school requires two topi ...

  5. cf 1324D. Pair of Topics

    D. Pair of Topics 题意:给定ab序列,问i<j且ai+aj>bi+bj的对数. 转化:ai-bi<-(aj-bj) 一开始拿到题目想着sort,但是发现i<j ...

  6. CodeForces - 1324D Pair of Topics(二分或双指针)

    题意:略 题记: 做法一:二分 #include<bits/stdc++.h>using namespace std; typedef long long ll; const int N= ...

  7. Codeforces 1324D Pair of Topics

    题目链接:https://codeforces.com/contest/1324/problem/D 题目描述 有两个长度为 n 的数组 A, B.问有多少对 (i,j) 满足 i < j 且 ...

  8. Educational Codeforces Round 80 (Rated for Div. 2) 二分 + 状压

    传送门 文章目录 题意: 思路: 题意: 给你nnn个长度为mmm的数组,选出两个来,让他们每一位取maxmaxmax构成新数组bbb,让后最大化bbb的最小值. 思路: 看到m=8m=8m=8,也就 ...

  9. Codeforces Round #627 (Div. 3) E - Sleeping Schedule (线性dp)

    **E Sleeping Schedule ** 题目描述 Vova had a pretty weird sleeping schedule. There are h hours in a day. ...

最新文章

  1. ICE专题:ICE起步
  2. unity android 启动,Android启动Unity
  3. 【MYSQL】常用命令备忘录
  4. win32汇编实现拼接SQL语句
  5. Java的agent机制简述
  6. java单词按字典排序_最终Java日志字典:开发人员最常记录的单词是什么?
  7. Luogu 4755 Beautiful Pair
  8. 花器官身份基因与靶基因间的调控进化情况
  9. sentinel接入网关应用_阿里sentinel配合gateway 网关限流
  10. angularjs内置63个指令
  11. virtualbox安装Windows 7 64位旗舰版 (包含镜像文件)
  12. Simscape Multibody --- 齿轮齿条约束
  13. java blog 引擎_推荐10个Java开源CMS系统
  14. python2个子线程等待_Python的并发并行[1] - 线程[3] - 多线程的同步控制
  15. 什么是域名?什么网站名?什么是URL?
  16. linux log4cxx 静态库,log4cxx的个人实践
  17. 工作时间如何安排:集中注意力的方法
  18. HDFS中block的大小
  19. 51nod 1298 圆与三角形(几何知识)
  20. Java【关于如何招聘靠谱的JAVA开发工程师】

热门文章

  1. Java打印完整的堆栈信息
  2. WiFi产品抗干扰设计
  3. matlab中removeback的意思,remove是什么意思
  4. js获取系统当前时间,实现钟表功能
  5. 怎样给证件照快速改底色?告诉大家一个简单方法
  6. 百度网盘ubuntu版deb包安装
  7. 33省市出台区块链专项政策,有地方拿户口、百万奖金抢人
  8. 分享篇:第十届“泰迪杯”数据挖掘挑战赛-农田害虫图像识别(特等奖)一
  9. 什么是云计算, 什么是 IaaS, PaaS, SaaS
  10. Unity项目-黑魂复刻(二)玩家控制器(跳跃)