D. Pair of Topics

time limit per test

2 seconds

memory limit per test

256 megabytes

The next lecture in a high school requires two topics to be discussed. The ii-th topic is interesting by aiai units for the teacher and by bibi units for the students.

The pair of topics ii and jj (i<ji<j) is called good if ai+aj>bi+bjai+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 nn (2≤n≤2⋅1052≤n≤2⋅105) --- the number of topics.

The second line of the input contains nn integers a1,a2,...,ana1,a2,...,an (1≤ai≤1091≤ai≤109), where aiai is the interestingness of the ii-th topic for the teacher.

The third line of the input contains nn integers b1,b2,...,bnb1,b2,...,bn (1≤bi≤1091≤bi≤109), where bibi is the interestingness of the ii-th topic for the students.

Output

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

Examples

Input

Copy

复制代码
5
4 8 2 6 2
4 5 4 1 3

Output

Copy

复制代码
7

Input

Copy

复制代码
4
1 3 2 4
1 3 2 4

Output

Copy

复制代码
0

解题说明:此题是一道数学题,可以先用数列a减去数列b得到数列c,对数列c进行排序,然后统计出数列c中两者之后大于0的对数就可以了。统计数列c的时候因为已经按从小到大排序了,可以每次以头尾两个数相加计算,然后逐渐靠近得到最终值。

cpp 复制代码
#include<bits/stdc++.h>
using namespace std;

int main()
{
	int n;
	cin >> n;
	long long int count = 0;
	int a[n];
	for (int i = 0; i < n; i++)
	{
		cin >> a[i];
	}
	for (int i = 0; i < n; i++)
	{
		int x;
		cin >> x;
		a[i] = a[i] - x;
	}
	sort(a, a + n);
	int l = 0, r = n - 1;
	while (l < r)
	{
		if (a[l] + a[r] > 0)
		{
			count += r - l;
			r--;
		}
		else
		{
			l++;
		}
	}
	cout << count << '\n';
	return 0;
}
相关推荐
Voyager_41 小时前
算法学习记录03——二叉树学习笔记:从两道题看透后序位置的关键作用
笔记·学习·算法
我搞slam6 小时前
快乐数--leetcode
算法·leetcode·哈希算法
WWZZ20257 小时前
快速上手大模型:机器学习3(多元线性回归及梯度、向量化、正规方程)
人工智能·算法·机器学习·机器人·slam·具身感知
东方佑8 小时前
从字符串中提取重复子串的Python算法解析
windows·python·算法
西阳未落8 小时前
LeetCode——二分(进阶)
算法·leetcode·职场和发展
通信小呆呆8 小时前
以矩阵视角统一理解:外积、Kronecker 积与 Khatri–Rao 积(含MATLAB可视化)
线性代数·算法·matlab·矩阵·信号处理
CoderCodingNo9 小时前
【GESP】C++四级真题 luogu-B4068 [GESP202412 四级] Recamán
开发语言·c++·算法
一个不知名程序员www9 小时前
算法学习入门---双指针(C++)
c++·算法
Shilong Wang10 小时前
MLE, MAP, Full Bayes
人工智能·算法·机器学习
Theodore_102210 小时前
机器学习(6)特征工程与多项式回归
深度学习·算法·机器学习·数据分析·多项式回归