zoukankan      html  css  js  c++  java
  • codeforces 1324 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 (2n2105) — the number of topics.

    The second line of the input contains nn integers a1,a2,,an (1ai109), 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 (1bi109), 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
    input
    Copy
    5
    4 8 2 6 2
    4 5 4 1 3
    
    output
    Copy
    7
    
    input
    Copy
    4
    1 3 2 4
    1 3 2 4
    
    output
    Copy
    0

    解题思路:

      由于 n 的大小限制,两个for循环必然超时。

     调整不等式  为  (Ai - Bi) + (Aj - Bj)  > 0 ,那么此时就可将两个数组转换为一个数组

    然后从小到大排序,利用二分,对于第i项,通过二分在[i+1,n]找到最小的j,满足该不等式,使用upper_bound函数即可

    #include<bits/stdc++.h>
    using namespace std;
    const int N = 1e6+10;
    int a[N],b[N],c[N],d[N];
    int main()
    {
        std::ios::sync_with_stdio(false);
        int n;
        cin>>n;
        for(int i = 1; i <= n; i++) {
            cin>>a[i];
        }
        for(int i = 1; i <= n; i++) {
            cin>>b[i];
            c[i] = a[i]-b[i];
        }
        sort(c+1,c+n+1);
        long long ans=0;
        for(int i=1;i<=n;i++){
            int pos=upper_bound(c+i+1,c+n+1,-c[i])-c-1;
            if(pos>=n+1)
                continue;
            ans += (n-pos);
        }
        cout<<ans<<endl;
        return 0;
    }

      

  • 相关阅读:
    CCF CSP 201709-1 打酱油 (贪心)
    CCF CSP 201712-1 最小差值
    CCF CSP 201612-1 中间数
    CCF CSP 201609-1 最大波动
    CCF CSP 201604-1 折点计数
    CCF CSP 201512-1 数位之和
    CCF CSP 201509-1 数列分段
    CCF CSP 201503-1 图像旋转 (降维)
    CCF CSP 201412-1 门禁系统
    CCF CSP 201409-1 相邻数对
  • 原文地址:https://www.cnblogs.com/clb123/p/12497946.html
Copyright © 2011-2022 走看看