当前位置:网站首页>CodeForces - 1324D Pair of Topics(二分或双指针)
CodeForces - 1324D Pair of Topics(二分或双指针)
2022-07-07 07:09:00 【moyangxian】
题意:略
题记:
做法一:二分
#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;
}
做法二:双指针
#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;
}
边栏推荐
- 进程和线程的区别
- Information Security Experiment 3: the use of PGP email encryption software
- What development models did you know during the interview? Just read this one
- Unity shader (basic concept)
- JS judge whether checkbox is selected in the project
- Niuke - Huawei question bank (61~70)
- PostgreSQL创建触发器的时候报错,
- Arthas simple instructions
- JS inheritance prototype
- Diffusion模型详解
猜你喜欢
随机推荐
Network request process
Redis common commands
Diffusion模型详解
[4G/5G/6G专题基础-146]: 6G总体愿景与潜在关键技术白皮书解读-1-总体愿景
4、 Fundamentals of machine learning
**grafana安装**
根据热门面试题分析Android事件分发机制(二)---事件冲突分析处理
【无标题】
C# Socke 服务器,客户端,UDP
flinkcdc 用sqlclient可以指定mysqlbinlog id执行任务吗
Regular matching starts with XXX and ends with XXX
How will fashion brands enter the meta universe?
如何成为一名高级数字 IC 设计工程师(1-6)Verilog 编码语法篇:经典数字 IC 设计
根据热门面试题分析Android事件分发机制(一)
Unity shader (to achieve a simple material effect with adjustable color attributes only)
【原创】程序员团队管理的核心是什么?
H5网页播放器EasyPlayer.js如何实现直播视频实时录像?
网易云微信小程序
PostgreSQL reports an error when creating a trigger,
Install pyqt5 and Matplotlib module