Codeforces Round #627 (Div. 3) D. Pair of Topics(二分/直接遍历)
Posted lipoicyclic
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 (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
5 4 8 2 6 2 4 5 4 1 3
7
4 1 3 2 4 1 3 2 4
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; }
以上是关于Codeforces Round #627 (Div. 3) D. Pair of Topics(二分/直接遍历)的主要内容,如果未能解决你的问题,请参考以下文章
Codeforces Round #627 (Div. 3) 补题
Codeforces Round #627 (Div. 3)
Codeforces Round #627 (Div. 3)(A--D)
Codeforces Round #627 (Div. 3) D. Pair of Topics(二分/直接遍历)
Codeforces Round #627 (Div. 3)F. Maximum White Subtree
Codeforces Round #627 (Div. 3) F - Maximum White Subtree(深度优先搜索)