我是靠谱客的博主 粗犷篮球,最近开发中收集的这篇文章主要介绍CodeForces - 1324D Pair of Topics(思维+二分),觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

题目链接: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

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

题意
给定一个数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即可。
代码

#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 - 1324D Pair of Topics(思维+二分)所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(49)

评论列表共有 0 条评论

立即
投稿
返回
顶部