Loading

Codeforces Round #627 (Div. 3) D. Pair of Topics(二分/直接遍历)

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 (2n21052≤n≤2⋅105 ) — the number of topics.

The second line of the input contains nn integers a1,a2,,ana1,a2,…,an (1ai1091≤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 (1bi1091≤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
这种题套路就是移一下项转化为ai-bi,然后先对ai-bi=ci进行排序。若要ci+cj>0,这两个起码要有一个为0,所以从大到小扫一遍,当ci大于0时,用upper_bound找到第一个大于-ci的数,这样一定能保证这个区间里的数的和大于0,把长度累加到答案里就行。
其实也可以双指针同时往中间移动,这么写啰嗦一点不过常数比较小....能过万岁
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
struct good
{
    int a,b;
}g[200005];
int res[200005];
int n;

int main()
{
    cin>>n;
    int i;
    for(i=1;i<=n;i++)scanf("%d",&g[i].a);
    for(i=1;i<=n;i++)scanf("%d",&g[i].b);
    for(i=1;i<=n;i++)res[i]=g[i].a-g[i].b;
    sort(res+1,res+n+1);
    long long ans=0;
    for(i=n;i>=1;i--)
    {
        if(res[i<0])break;
        int pos=upper_bound(res+1,res+i+1,-res[i])-res;
        if(pos>=1&&pos<i)
        {
            ans+=i-pos;
        }
    }
    cout<<ans;
 } 

 



posted @ 2020-03-29 00:42  脂环  阅读(251)  评论(0编辑  收藏  举报