B - Pair of Topics

CodeForces – 1324D 

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.ExamplesInput

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>bj-aj,然后想预处理两个差,进行一些操作。发现不能搞并且这样还是O(n^2)

正确思路:转化成(ai-bi)+(aj-bj)>0来做;转化到这一步i和j就是同一个序列的操作了。也就是ai-bi的差值实际上和aj-bj的差值是一样的序列里面的,就可以O(n)处理问题了

启发:等式枚举的转化要往变成同一序列去转化

#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn=2e5+100;
typedef long long LL;
LL a[maxn],b[maxn];
int main(void)
{
  cin.tie(0);std::ios::sync_with_stdio(false);
  LL n;cin>>n;
  for(LL i=1;i<=n;i++) cin>>a[i];
  for(LL i=1;i<=n;i++) cin>>b[i];
  for(LL i=1;i<=n;i++){
  	b[i]=a[i]-b[i];
  }
  sort(b+1,b+1+n);
  LL l=1;LL r=n;
  //双指针 
  LL sum=0;
  while(l<r){
  	if(b[r]+b[l]>0){
  		sum+=r-l;
		r--;	
	}
	else l++;
  }
  cout<<sum<<endl;
return 0; 
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值