训练——F()
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 (2≤n≤2⋅1052≤n≤2⋅105) — the number of topics.
The second line of the input contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤1091≤ai≤109), where aiai is the interestingness of the ii-th topic for the teacher.
The third line of the input contains nn integers b1,b2,…,bnb1,b2,…,bn (1≤bi≤1091≤bi≤109), 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
5
4 8 2 6 2
4 5 4 1 3
Output
7
Input
4
1 3 2 4
1 3 2 4
Output
0
题解,求a[i]+a[j]>b[i]+b[j]的个数,我们可以转换为
令 c[i]=a[i]-a[j],即是求c[i]>-c[j],解法一 求c[i]+c[j]>0
排序并无影响,定义两指针,从两头相加,如果,c[左]+c[右]>0,因为是升序,那么符合的数为,右减左。
#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; #define ll long long const int N=200005; int n,a[N],b[N],c[N]; int main() { scanf("%d",&n); for(int i=0; i<n; i++) scanf("%d",&a[i]); for(int i=0; i<n; i++) scanf("%d",&b[i]); for(int i=0; i<n; i++) c[i]=a[i]-b[i]; sort(c,c+n); ll ans=0; int l=0,r=n-1; while(l<r) { if(c[l]+c[r]>0) { if(l>=r) break; ans=ans+(r-l); r--; } else l++; } printf("%lld\n",ans); return 0; }
解法二
:将数组c递增排序,变形一下原式为 -ci < cj,我们对于每个ci,往后直接二分查找大于 -ci 的数有多少个就可以了;
那个啥,我连二分都不会,代码等我会了再写。
- 条件随机场(4)——CRF++分词训练和预测
- 通用编程能力训练:Memory Track(2012-01-29 19:02)
- 论文解析:人脸检测中级联卷积神经网络的联合训练
- 蓝桥杯 算法训练 回文数 【模拟 + stringsteam的运用】
- pku 3295 Tautology 第一周训练——递归+枚举
- 使用docker安装部署Spark集群来训练CNN(含Python实例)
- 深度学习框架里使用GPU训练网络参数时查看GPU的使用情况
- 【原创】科研训练指导手册(DE2-115_labs_vhdl)-PART6--实验五
- 【转】ACM训练计划
- 超火的漫画线稿上色AI出新版了!无监督训练,效果更美好 | 代码+Demo
- 【Algorithm】转一个ACM训练计划
- 百度PRNN:增强GPU伸缩性,RNN训练最高提速30倍(源码下载)
- 使用Google word2vec训练我们自己的词向量模型
- 弥补胸肌缺陷的训练策略
- 训练4 习题6
- 马里兰大学帕克分校提出对「损失函数」进行「可视化」,以提高神经网络的训练能力
- 4.12训练_P4
- Caffe学习系列(12):训练和测试自己的图片
- python下word2vec词向量训练与加载方法
- opencv haar训练--训练样本(4)