当前位置:网站首页>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;
}
边栏推荐
- PHP curl post length required error setting header header
- R语言使用lm函数构建多元回归模型(Multiple Linear Regression)、使用step函数构建前向逐步回归模型筛选预测变量的最佳子集、scope参数指定候选预测变量
- 【AI4Code】《CodeBERT: A Pre-Trained Model for Programming and Natural Languages》 EMNLP 2020
- 创新突破!亚信科技助力中国移动某省完成核心账务数据库自主可控改造
- Go 垃圾回收器指南
- Brpc source code analysis (VI) -- detailed explanation of basic socket
- 【Debias】Model-Agnostic Counterfactual Reasoning for Eliminating Popularity Bias in RS(KDD‘21)
- Word中的空白页,怎么也删不掉?如何操作?
- PHP curl post x-www-form-urlencoded
- Innovation and breakthrough! AsiaInfo technology helped a province of China Mobile complete the independent and controllable transformation of its core accounting database
猜你喜欢

Go garbage collector Guide

dirReader. Readentries compatibility issues. Exception error domexception

Innovation and breakthrough! AsiaInfo technology helped a province of China Mobile complete the independent and controllable transformation of its core accounting database

知识图谱用于推荐系统问题(MVIN,KERL,CKAN,KRED,GAEAT)

剑指 Offer 22. 链表中倒数第k个节点

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

Word中的空白页,怎么也删不掉?如何操作?

php curl post Length Required 错误设置header头

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

Power Bi -- these skills make the report more "compelling"“
随机推荐
Sword finger offer 22. the penultimate node in the linked list
小程序image 无法显示base64 图片 解决办法 有效
[MySQL 17] installation exception: could not open file '/var/log/mysql/mysqld log‘ for error logging: Permission denied
Brpc source code analysis (VI) -- detailed explanation of basic socket
氢能创业大赛 | 国家能源局科技司副司长刘亚芳:构建高质量创新体系是我国氢能产业发展的核心
Transformer variants (spark transformer, longformer, switch transformer)
【GCN-RS】MCL: Mixed-Centric Loss for Collaborative Filtering (WWW‘22)
Meta learning (meta learning and small sample learning)
Power BI----这几个技能让报表更具“逼格“
基于TCP/IP在同一局域网下的数据传输
【AI4Code】CodeX:《Evaluating Large Language Models Trained on Code》(OpenAI)
PHP curl post x-www-form-urlencoded
Web programming (II) CGI related
brpc源码解析(二)—— brpc收到请求的处理过程
How to solve the problem of the error reported by the Flink SQL client when connecting to MySQL?
GPT plus money (OpenAI CLIP,DALL-E)
R语言组间均值是否相同的成对比较:使用pairwise.t.test函数执行多个分组数据均值的两两成对假设检验
【云驻共创】AI在数学界有哪些作用?未来对数学界会有哪些颠覆性影响?
Eureka注册中心开启密码认证-记录
通过Referer请求头实现防盗链