当前位置:网站首页>Codeforces - 1324d pair of topics

Codeforces - 1324d pair of topics

2022-07-07 09:47:00 moyangxian

The question : A little

. :

Practice one : Two points

#include<bits/stdc++.h>

using namespace std;
typedef long long ll;
const int N=2e5+10;
int a[N],b[N],c[N];

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+1+n);
    ll ans=0;
    for(int i=1;i<=n;i++){
    
        if(c[i]<=0) continue;
        int p=upper_bound(c+1,c+1+n,-c[i])-c;
        ans+=i-p;
    }
    cout<<ans<<endl;
    return 0;
}

Practice two : Double pointer

#include<bits/stdc++.h>

using namespace std;
typedef long long ll;
const int N=2e5+10;
int a[N],b[N],c[N];

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+1+n);
    ll ans=0;
    int l=1,r=n;
    while(l<r){
    
        if(c[l]+c[r]>0){
    
            ans+=r-l;
            r--;
        }
        else l++;
    }
    cout<<ans<<endl;
    return 0;
}

原网站

版权声明
本文为[moyangxian]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207070709202059.html