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;
}
相关推荐
YuTaoShao3 小时前
【LeetCode 热题 100】141. 环形链表——快慢指针
java·算法·leetcode·链表
小小小新人121234 小时前
C语言 ATM (4)
c语言·开发语言·算法
你的冰西瓜4 小时前
C++排序算法全解析(加强版)
c++·算法·排序算法
এ᭄画画的北北4 小时前
力扣-31.下一个排列
算法·leetcode
绝无仅有5 小时前
企微审批对接错误与解决方案
后端·算法·架构
用户5040827858396 小时前
1. RAG 权威指南:从本地实现到生产级优化的全面实践
算法
Python×CATIA工业智造7 小时前
详细页智能解析算法:洞悉海量页面数据的核心技术
爬虫·算法·pycharm
无聊的小坏坏8 小时前
力扣 239 题:滑动窗口最大值的两种高效解法
c++·算法·leetcode
黎明smaly8 小时前
【排序】插入排序
c语言·开发语言·数据结构·c++·算法·排序算法