您的位置:首页 > 其它

训练——F()

2020-05-11 04:11 597 查看

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
5
4 8 2 6 2
4 5 4 1 3
Output
7
Input
4
1 3 2 4
1 3 2 4
Output
0
题解,求a[i]+a[j]>b[i]+b[j]的个数,我们可以转换为
令 c[i]=a[i]-a[j],即是求c[i]>-c[j],解法一 求c[i]+c[j]>0
排序并无影响,定义两指针,从两头相加,如果,c[左]+c[右]>0,因为是升序,那么符合的数为,右减左。

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define ll long long
const int N=200005;
int  n,a[N],b[N],c[N];
int main()
{
scanf("%d",&n);
for(int i=0; i<n; i++)
scanf("%d",&a[i]);
for(int i=0; i<n; i++)
scanf("%d",&b[i]);
for(int i=0; i<n; i++)
c[i]=a[i]-b[i];
sort(c,c+n);
ll ans=0;
int l=0,r=n-1;
while(l<r)
{
if(c[l]+c[r]>0)
{
if(l>=r)
break;
ans=ans+(r-l);
r--;
}
else
l++;
}
printf("%lld\n",ans);
return 0;

}

解法二
:将数组c递增排序,变形一下原式为 -ci < cj,我们对于每个ci,往后直接二分查找大于 -ci 的数有多少个就可以了;
那个啥,我连二分都不会,代码等我会了再写。

Starry_Sky_Dream 原创文章 50获赞 4访问量 2347 关注 私信
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: