题目链接:https://vjudge.net/contest/362265#problem/D
The next lecture in a high school requires two topics to be discussed. The i-th topic is interesting by aiai 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 nn integers a1,a2,…,an (1≤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,…,bn (1≤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.
Input
1
2
35 4 8 2 6 2 4 5 4 1 3
Output
17
Input
1
2
34 1 3 2 4 1 3 2 4
Output
10
题意:
给定一个数n
第二行n个数,表示an
第三行n个数,表示bn
求满足i<j时,ai+aj>bi+bj的对数。
分析:
进行变形,ai-bi+aj-bj>0
用p[i]表示ai-bi
将p[i]从小到大排序。要满足p[i]+p[j]>0,其中一个肯定要大于0。假如p[i]>0,则p[j]只要大于等于-p[i]+1同时满足下标小于j即可。
代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30#include<cstdio> #include<cstring> #include<algorithm> using namespace std; typedef long long LL; const int N=2*1e5+10; int a[N],b[N],p[N]; int n; int main() { scanf("%d",&n); for(int i=1; i<=n; i++) scanf("%d",&a[i]); for(int i=1; i<=n; i++) scanf("%d",&b[i]); for(int i=1; i<=n; i++) p[i]=a[i]-b[i]; sort(p+1,p+1+n); int sum=0; for(int i=1; i<=n; i++) { if(p[i]<=0) continue; int pos=lower_bound(p+1,p+1+n,-p[i]+1)-p; sum+=i-pos; } printf("%dn",sum); return 0; }
最后
以上就是粗犷篮球最近收集整理的关于CodeForces - 1324D Pair of Topics(思维+二分)的全部内容,更多相关CodeForces内容请搜索靠谱客的其他文章。
发表评论 取消回复