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;
}
相关推荐
im_AMBER11 分钟前
算法笔记 18 二分查找
数据结构·笔记·学习·算法
C雨后彩虹22 分钟前
机器人活动区域
java·数据结构·算法·华为·面试
MarkHD36 分钟前
车辆TBOX科普 第53次 三位一体智能车辆监控:电子围栏算法、驾驶行为分析与故障诊断逻辑深度解析
算法
苏小瀚1 小时前
[算法]---路径问题
数据结构·算法·leetcode
月明长歌2 小时前
【码道初阶】一道经典简单题:多数元素(LeetCode 169)|Boyer-Moore 投票算法详解
算法·leetcode·职场和发展
wadesir2 小时前
C语言模块化设计入门指南(从零开始构建清晰可维护的C程序)
c语言·开发语言·算法
t198751282 小时前
MATLAB水声信道仿真程序
开发语言·算法·matlab
CoderYanger3 小时前
动态规划算法-简单多状态dp问题:15.买卖股票的最佳时机含冷冻期
开发语言·算法·leetcode·动态规划·1024程序员节
Xの哲學3 小时前
Linux RTC深度剖析:从硬件原理到驱动实践
linux·服务器·算法·架构·边缘计算
狐573 小时前
2025-12-04-牛客刷题笔记-25_12-4-质数统计
笔记·算法