Codeforces Round #627 (Div. 3) D.Pair of Topics

D. 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 8 2 6 2
4 5 4 1 3
outputCopy
7
inputCopy
4
1 3 2 4
1 3 2 4
outputCopy
0

题意:就是找题中那个式子满足的对数有多少

思路:移向一下考虑 (ai-bi)+(aj-bj)>0
其实也就是对于相同位置的a和b进行作差为c 然后看后面有没有下标j使得当前位置i
ci+cj>0即可
比如第一个样例 作差后 0 3 -2 5 -1
题上有说要j>i 我们排序后 只看后面有多少满足即可
排序后-2 -1 0 3 5
对于-2 有 3 5 使得和大于0
对于-1有 3 5
对于0 有3 5
对于3 有5
所以答案是7
因为排序了 如果当前是正数 直接加上后面的个数即可 如果非正数 二分一下一个下标j使得ai+aj>0 那么满足的个数就是n-j+1

#include<bits/stdc++.h>
using namespace std;
#define ll long long;
ll a[200005],b[200005],
ll cha[200005];
int main()
{
    
    int n;cin>>n;
    vector<int> a(n+1),b(n+1);
    repd(i,1,n) cin>>a[i];
    repd(i,1,n) cin>>b[i];
    vector<ll> c(n+1);
    repd(i,1,n) c[i]=a[i]-b[i];
    sort(c.begin()+1,c.end());
    ll ans=0;
    repd(i,1,n){
       if(c[i]<=0){
         int k=upper_bound(c.begin()+1,c.end(),-c[i])-c.begin();
         ans+=n-k+1;
       }
       else ans+=n-i;
    }
    cout<<ans<<endl;
    return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我不会c语言

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值