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;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章