当前位置:网站首页>Median (二分答案 + 二分查找)
Median (二分答案 + 二分查找)
2022-07-25 11:14:00 【梦中醉卧惊起】
Median
题意:
有N个整数,计算两两不同的整数间的差值,然后找到差值中的中位数.
思路:
二分答案 + 二分查找
题目数据范围是xi <= 1e9,N <= 1e5;如果直接暴力1e10的复杂度必爆;1e9数据需要longlong处理;
二分枚举中位数的可能值,然后二分查找满足条件的个数;
这里我们找的个数是比中位数小的数的个数,这个式子中一共有n * (n - 1) / 2个数 那么偶数个的中位数就是 n * (n - 1) / 4 ,奇数就是n * (n - 1) / 4 + 1;
直接给每个值加上中位数,然后upper_bound统计有多少个比中位数值小的数即可,最后看是否满足一半的个数即可
根据取大取小,合理使用lower_bound和注意往那一边进行搜索
AC代码:
取大的
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
typedef long long ll;
const int N = 1e5 + 10;
int a[N];
ll n, m;
bool check(int x){
ll sum = 0;
for(int i = 0;i < n;i ++){
int pos = n - (upper_bound(a,a + n,a[i] + x) - a);
sum += pos;
}
return sum <= m;
}
int main(){
while(~scanf("%d",&n)){
for(int i = 0;i < n;i ++) scanf("%d",&a[i]);
sort(a,a + n);
m = n * (n - 1) / 4;
// if(m & 1) m = m / 2;
// else m = m / 2;
int l = -1,r = a[n - 1] - a[0];
while(l < r){
int mid = (l + r) >> 1;
if(check(mid)) r = mid;
else l = mid + 1;
}
printf("%d\n",l);
}
return 0;
}
取小的
# include <iostream>
# include<cstdio>
# include <algorithm>
using namespace std;
typedef long long ll;
const int N = 1e5 + 10;
int a[N];
ll n, m;
bool check(int x){
ll sum = 0;
for(int i=0; i<n-1; ++i){ //统计有多少个比x小
int pos = upper_bound(a+i, a+n, a[i]+x)-a;
sum += pos - i - 1;
if(sum >= m)
return true;
}
return false;
}
int main(){
while(~scanf("%lld",&n)){
for(int i=0; i<n; ++i)
scanf("%d",&a[i]);
ll tmp = n*(n-1)>>1;
if(tmp&1)
m = (tmp>>1)+1;
else
m = tmp>>1;
sort(a, a+n);
int l=0, r=a[n-1]-a[0], mid;
while(l<r)
{
int mid = (l + r)>>1;
if(check(mid))
r = mid;
else
l = mid+1;
}
printf("%d\n",r);
}
return 0;
}
边栏推荐
- 基于TCP/IP在同一局域网下的数据传输
- PHP curl post x-www-form-urlencoded
- brpc源码解析(八)—— 基础类EventDispatcher详解
- What is the global event bus?
- 奉劝那些刚参加工作的学弟学妹们:要想进大厂,这些并发编程知识是你必须要掌握的!完整学习路线!!(建议收藏)
- 【AI4Code】《Contrastive Code Representation Learning》 (EMNLP 2021)
- 【多模态】《HiT: Hierarchical Transformer with Momentum Contrast for Video-Text Retrieval》ICCV 2021
- 【高并发】我用10张图总结出了这份并发编程最佳学习路线!!(建议收藏)
- 【CTR】《Towards Universal Sequence Representation Learning for Recommender Systems》 (KDD‘22)
- The JSP specification requires that an attribute name is preceded by whitespace
猜你喜欢

【Debias】Model-Agnostic Counterfactual Reasoning for Eliminating Popularity Bias in RS(KDD‘21)

Application of comparative learning (lcgnn, videomoco, graphcl, XMC GaN)

Solutions to the failure of winddowns planning task execution bat to execute PHP files

JS operator

【GCN-RS】Region or Global? A Principle for Negative Sampling in Graph-based Recommendation (TKDE‘22)

【AI4Code】《Pythia: AI-assisted Code Completion System》(KDD 2019)

JS数据类型以及相互转换

Transformer variants (spark transformer, longformer, switch transformer)

brpc源码解析(一)—— rpc服务添加以及服务器启动主要过程

brpc源码解析(八)—— 基础类EventDispatcher详解
随机推荐
Transformer变体(Sparse Transformer,Longformer,Switch Transformer)
银行理财子公司蓄力布局A股;现金管理类理财产品整改加速
【6篇文章串讲ScalableGNN】围绕WWW 2022 best paper《PaSca》
【无标题】
【GCN-CTR】DC-GNN: Decoupled GNN for Improving and Accelerating Large-Scale E-commerce Retrieval WWW22
Javescript loop
对比学习的应用(LCGNN,VideoMoCo,GraphCL,XMC-GAN)
R语言使用lm函数构建多元回归模型(Multiple Linear Regression)、使用step函数构建前向逐步回归模型筛选预测变量的最佳子集、scope参数指定候选预测变量
PHP uploads the FTP path file to the curl Base64 image on the Internet server
Intelligent information retrieval(智能信息检索综述)
[high concurrency] a lock faster than read-write lock in high concurrency scenarios. I'm completely convinced after reading it!! (recommended Collection)
OSPF综合实验
【Debias】Model-Agnostic Counterfactual Reasoning for Eliminating Popularity Bias in RS(KDD‘21)
flink sql client 连接mysql报错异常,如何解决?
PHP 上传ftp路径文件到外网服务器上 curl base64图片
任何时间,任何地点,超级侦探,认真办案!
微星主板前面板耳机插孔无声音输出问题【已解决】
【USB设备设计】--复合设备,双HID高速(64Byte 和 1024Byte)
php curl post Length Required 错误设置header头
程序员送给女孩子的精美礼物,H5立方体,唯美,精致,高清