Codeforces 1324 D-Pair of Topics (thinking + || half the double pointer)

Pair of Topics

time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
The next lecture in a high school requires two topics to be discussed. The i-th topic is interesting by ai units for the teacher and by bi units for the students.

The pair of topics i and j (i<j) is called good if ai+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 n (2≤n≤2⋅105) — the number of topics.

The second line of the input contains n integers a1,a2,…,an (1≤ai≤109), where ai is the interestingness of the i-th topic for the teacher.

The third line of the input contains n integers b1,b2,…,bn (1≤bi≤109), where bi is the interestingness of the i-th topic for the students.

Output
Print one integer — the number of good pairs of topic.

Examples
inputCopy

. 5
. 4. 6 2 2. 8
. 4. 5. 4. 3. 1
outputCopy
. 7
inputCopy
. 4
. 1 2. 4. 3
. 1. 3 2. 4
outputCopy
0
question is intended:
to give you two arrays a [i] and b [i], the number of Q ( i, j) satisfies i <j and a [i] + a [j ]> b [i] + b [j].
Thinking a: half
can be simplified equation, becomes a [i] + a [j ] -b [i] -b [j]> 0, corresponds to a [i] -b [i] + (b [j] -a [j]) > 0.
May be to open a new array c [i] = a [i ] -b [i], then the problem is transformed into: Looking for a set of numbers in the array c, such that it satisfies i <j and c [i] + c [j]> 0
this time there are two solutions:
First, two for loops to direct enumeration, complexity is n ^ 2, apparent to this question, 2e5 * 2e5 desirable.

Second, the second layer for cycle optimization, we can sort in ascending order of the array c, the above equation can be turned into c [j]> -c [i ], then we can direct binary search of the second layer when the first for loop greater than -c [i] tmp position number, then the location of this location and meaning of the questions after a certain compliance, i.e., sum + = n-tmp + 1
; however noted here that c [i]> 0, because the lookup is -c [i], so c [i] also meet the search criteria, but does not meet the meaning of the questions, we have to go out of this situation.
Finally, note that we each have a pair of reverse calculation twice, the results need to / 2 and then output. Such complexity is n * lgn.
The final Finally, note that in this line of thinking, will burst int, so use long long count.
Binary code is:

#include<bits/stdc++.h>
using namespace std;
const int maxn=1e6+7;
typedef long long ll;
ll n,a[maxn],sum;
int main(){
    cin>>n;
    int x;
    for(int i=1;i<=n;i++) cin>>a[i];
    for(int i=1;i<=n;i++){
        cin>>x;
        a[i]-=x;
    }
    sort(a+1,a+1+n);
    for(int i=1;i<=n;i++) cout<<a[i]<<" ";
    puts("");
    for(int i=1;i<=n;i++){
        int tmp=upper_bound(a+1,a+1+n,-a[i])-a;
        sum+=n-tmp+1;
		if (a[i]>0) sum--;
		 //cout<<i<<" "<<tmp<<" "<<sum<<endl;//检测输出
    }
    cout<<sum/2;
    return 0;
}

Two ideas: the double pointer
in general, can be divided in two by the double pointer can be resolved, substantially the same degree of complexity -
double pointer Code:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=1e6+7;
int n,a[maxn],x,l,r;
ll ans;
int main()
{
	scanf("%d",&n);
	r=n;
	for(int i=1;i<=n;i++) cin>>a[i];
	for(int i=1;i<=n;i++) cin>>x,a[i]-=x;
	sort(a+1,a+n+1);
	for(int l=1;l<=n;l++)
	{
		while(a[l]+a[r]>0)r--;///找最后一个满足条件的位置
		ans+=n-max(l,r);//该位置之后的一定满足条件
	}
	printf("%lld",ans);
	return 0;
}

Done before in cattle off a similar problem, temporarily not find the first pair of such, this problem can be solved again and again about the qwq time to write a solution to a problem only to find himself pushed wrong. .
There is also a common solution, when the monotonicity can not have a tree or array segment tree maintenance, to be completed.

Published 48 original articles · won praise 7 · views 3314

Guess you like

Origin blog.csdn.net/weixin_45675097/article/details/104831297