题意:略
题记:
做法一:二分
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23#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; }
做法二:双指针
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26#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; }
最后
以上就是苗条心锁最近收集整理的关于CodeForces - 1324D Pair of Topics(二分或双指针)的全部内容,更多相关CodeForces内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复