codeforces:D - Pair of Topics(1.新思路解题,2.STL二分法解题。)

问题:
The next lecture in a high school requires two topics to be discussed. The ii ii-th topic is interesting by aiai a_iai​ units for the teacher and by bibi b_ibi​ units for the students.
The pair of topics ii ii and j(i<j)j(i<j) j (i<j)j(i<j) is called good if ai+aj>bi+bjai+aj>bi+bj a_i+a_j>b_i+b_jai​+aj​>bi​+bj​ (i.e. it is more interesting for the teacher).
Your task is to find the number of good pairs of topics.
输入:
The first line of the input contains one integer nn nn (2≤n≤2⋅105)(2≤n≤2⋅105) (2≤n≤2⋅10^5)(2≤n≤2⋅105) — the number of topics.
The second line of the input contains nn nn integers a1,a2,…,an(1≤ai≤109)a1,a2,…,an(1≤ai≤109) a_1,a_2,…,a_n (1≤a_i≤10^9)a1​,a2​,…,an​(1≤ai​≤109), where aiai a_iai​ is the interestingness of the ii ii-th topic for the teacher.
The third line of the input contains nn nn integers b1,b2,…,bn(1≤bi≤109)b1,b2,…,bn(1≤bi≤109) b_1,b_2,…,b_n (1≤b_i≤10^9)b1​,b2​,…,bn​(1≤bi​≤109), where bibi b_ibi​ is the interestingness of the ii ii-th topic for the students.
输出:
Print one integer — the number of good pairs of topic.
①:
input example:
5
4 8 2 6 2
4 5 4 1 3
output example:
7
②:
input example:
4
1 3 2 4
1 3 2 4
output example
0
这题是要求ai-bi>aj-bj,其中j>i。就这个j>i,我当时就被困惑住了,其实这只是说明ai不能等于aj,就是不能重复。哎,还是比赛少。
这题思路是将ai-bi=ci,在定义出来一个ci来表示,这样只用求c数列中有多少个不重复的两个数相加大于零就行了。这里求有多少个不重复的两个数相加大于零是一个难点(对于我来说),因为容易超时。
这里有个思路:

首先将c数组从大到小排序,并再定义一个j从n开始向左(在遍历每一个i时)判断j是否满足c[i]+c[j]<=0&&i<j满足的话就让j--。这样就会减到c[i]+c[j]>0,并且注意c数列是从大到小排序的,因此从i到j都满足c[i]+c[j]>0,那么就每次加上一个j-i,注意在加上j-i之前判断是否j>i,否则j-i就会小于零,同时不满足题中所给的j>i的条件,注意在每次遍历一个i之后不用让j=n哦!如果你想让j=n就说明你的思路是错的,注意这是个从大到小的数列。
而这种思路就可以大大减少时间复杂度。这个思路一定要记住哦!

下面是该思路的代码。代码中有注释细节需要注意哦!

#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=2e5+10;//这里定义了const int maxn中的maxn
//一定要要在main函数上面用,在main函数中用就会出错,反正
//在我用的编译器中是这样的,要注意,应该是maxn开太大,不
//能在main中用
int a[maxn],b[maxn],c[maxn];
int main()
{
    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,greater<int>());
    int j=n;
    long long ans=0;
    for(int i=1;i<=n;i++)
    {
        while(c[i]+c[j]<=0&&i<j)j--;
        if(j<=i)break;
        ans+=j-i;//这里也可以定义long long 为 ll,
        //然后ans+=1ll*j-i,是为了实现int 到long long 
        //这个应该更正确。
    }
    cout<<ans;
    return 0;
}

下面是用STL的upper_bound()函数解题,

思路:同样是求出c[i]=a[i]-b[i],这里的c数组用sort从小到大排序。然后用upper_bound()求出每个-c[i]的值这里很牛皮:
①upper_bound(a+i,a+n+1,-c[i])-a,这样写返回的是int型大于-ci中的最小值的下标。
②*upper_bound(和上面括号内一致)返回的是数组中大于c[i]的最小值,不是下标。
所以这里用j=①中的式子,ans+=(n-j+1)*1ll;(转longlong)。
最后输出,牛皮简单,这个要记住上面的新思路也要记住。

代码如下:

#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
const int maxn=2e5+10;
int a[maxn],b[maxn],c[maxn];
int main()
{
    int n;cin>>n;
    for(int i=1;i<=n;i++)cin>>a[i];
    for(int i=1;i<=n;i++)cin>>b[i];
    for(int i=1;i<=n;i++)c[i]=a[i]-b[i];
    sort(c+1,c+n+1);
    ll ans=0;
    for(int i=1;i<n;i++)
    {
        int j=upper_bound(c+i+1,c+n+1,-c[i])-c;
        ans+=(n-j+1)*1ll;
    }
    cout<<ans<<endl;
//int a[]={10,11,12,13,14};
//int x=upper_bound(a,a+5,11)-a;
//cout<<x;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值