当前位置:网站首页>Median (two point answer + two point search)
Median (two point answer + two point search)
2022-07-25 12:11:00 【Lie drunk and wake up in a dream】
Median
The question :
Yes N It's an integer , Calculate the difference between two different integers , Then find the median of the difference .
Ideas :
Two points answer + Two points search
The subject data range is xi <= 1e9,N <= 1e5; If direct violence 1e10 The complexity of will explode ;1e9 Data needs to be longlong Handle ;
Possible values of median of binary enumeration , Then find the number that meets the condition in two ;
The number we are looking for here is the number of numbers smaller than the median , There are n * (n - 1) / 2 Number Then the median of even numbers is n * (n - 1) / 4 , Odd number is n * (n - 1) / 4 + 1;
Directly add the median to each value , then upper_bound Count how many numbers are smaller than the median value , Finally, see whether half the number is satisfied
Choose the big and the small , The rational use of lower_bound And pay attention to which side to search
AC Code :
Take large
#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;
}
Take the small one
# 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){ // How many ratios are there x Small
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;
}
边栏推荐
- 【AI4Code】《Unified Pre-training for Program Understanding and Generation》 NAACL 2021
- 【多模态】《TransRec: Learning Transferable Recommendation from Mixture-of-Modality Feedback》 Arxiv‘22
- R语言ggpubr包ggarrange函数将多幅图像组合起来、annotate_figure函数为组合图像添加注释、注解、标注信息、fig.lab参数添加图像标签、fig.lab.face参数指定样式
- web编程(二)CGI相关
- Scott+Scott律所计划对Yuga Labs提起集体诉讼,或将确认NFT是否属于证券产品
- NLP知识----pytorch,反向传播,预测型任务的一些小碎块笔记
- 氢能创业大赛 | 国家能源局科技司副司长刘亚芳:构建高质量创新体系是我国氢能产业发展的核心
- GPT plus money (OpenAI CLIP,DALL-E)
- [comparative learning] understanding the behavior of contractual loss (CVPR '21)
- 【黑马早报】运营23年,易趣网宣布关停;蔚来对大众CEO抛出橄榄枝;华为天才少年曾放弃360万年薪;尹烨回应饶毅炮轰其伪科学...
猜你喜欢

PHP curl post x-www-form-urlencoded

【GCN-RS】Are Graph Augmentations Necessary? Simple Graph Contrastive Learning for RS (SIGIR‘22)

OSPF comprehensive experiment

【黑马早报】运营23年,易趣网宣布关停;蔚来对大众CEO抛出橄榄枝;华为天才少年曾放弃360万年薪;尹烨回应饶毅炮轰其伪科学...

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

Heterogeneous graph neural network for recommendation system problems (ackrec, hfgn)

创新突破!亚信科技助力中国移动某省完成核心账务数据库自主可控改造

Application and innovation of low code technology in logistics management
![[comparative learning] understanding the behavior of contractual loss (CVPR '21)](/img/96/9b58936365af0ca61aa7a8e97089fe.png)
[comparative learning] understanding the behavior of contractual loss (CVPR '21)

【AI4Code】《Contrastive Code Representation Learning》 (EMNLP 2021)
随机推荐
LeetCode 50. Pow(x,n)
【GCN】《Adaptive Propagation Graph Convolutional Network》(TNNLS 2020)
Hardware connection server TCP communication protocol gateway
从云原生到智能化,深度解读行业首个「视频直播技术最佳实践图谱」
The applet image cannot display Base64 pictures. The solution is valid
A beautiful gift for girls from programmers, H5 cube, beautiful, exquisite, HD
php curl post Length Required 错误设置header头
利用wireshark对TCP抓包分析
Eureka注册中心开启密码认证-记录
NLP知识----pytorch,反向传播,预测型任务的一些小碎块笔记
Multi label image classification
【AI4Code最终章】AlphaCode:《Competition-Level Code Generation with AlphaCode》(DeepMind)
Intelligent information retrieval(智能信息检索综述)
'C:\xampp\php\ext\php_ zip. Dll'-%1 is not a valid Win32 Application Solution
RestTemplate与Ribbon简单使用
PHP uploads the FTP path file to the curl Base64 image on the Internet server
Innovation and breakthrough! AsiaInfo technology helped a province of China Mobile complete the independent and controllable transformation of its core accounting database
brpc源码解析(一)—— rpc服务添加以及服务器启动主要过程
LeetCode第303场周赛(20220724)
Transformer变体(Routing Transformer,Linformer,Big Bird)