当前位置:网站首页>(POJ - 3579) median (two points)
(POJ - 3579) median (two points)
2022-07-06 16:09:00 【AC__ dream】
Chinese question meaning : Now there is N A mysterious number (A1,A2,A3….An), We need to get a password from this string of numbers .
The way to get the password is :
First calculate the difference between each number :|Ai-Aj|(1<=i<j<=N), In the end, you can get C Difference .
The final password is the median of these numbers .
If C It's even , It is stipulated that the median is C/2 Small difference .
This question and POJ-3579 The method of question is similar , We also solve it by dichotomy , For each number to be bisected x, We go through check Function to determine whether it is less than or equal to x How many pairs of distances are there , If the distance is less than or equal to x The number of pairs of is greater than half of the logarithm of the total , that r=x, conversely l=x+1, How should the specific dichotomy be carried out ? Let's start with a Sort the array from small to large , The dichotomy is found in ai On the right and the distance is less than or equal to x The number of , because aj-ai It is monotonically increasing , So this process is carried out through dichotomy , Traverse in turn ai, The distance is less than x Number to number , Two points are enough , There are quite a lot of details , See the code for details :
#include<cstdio>
#include<iostream>
#include<cstring>
#include<vector>
#include<algorithm>
#include<map>
#include<cmath>
#include<queue>
using namespace std;
const int N=1000003;
int a[N],n;
bool check(int x)// Find that the distance is less than or equal to x How many pairs are there , More than half the logarithm of all numbers returns true, Otherwise return to false
{
int cnt=0;
for(int i=1;i<=n;i++)
cnt+=upper_bound(a+1,a+n+1,a[i]+x)-a-i-1;
if(cnt>=((long long)n*(n-1)/2+1)/2) return true;
return false;
}
int main()
{
while(scanf("%d",&n)!=EOF)
{
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
sort(a+1,a+n+1);
int l=0,r=1000000000,mid;
while(l<r)
{
mid=l+r>>1;
if(check(mid)) r=mid;
else l=mid+1;
}
printf("%d\n",l);
}
return 0;
}
边栏推荐
- PySide6 信号、槽
- Penetration test (4) -- detailed explanation of meterpreter command
- Basic Q & A of introductory C language
- Openwrt build Hello ipk
- 基于web的照片数码冲印网站
- 409. Longest palindrome
- E. Breaking the Wall
- 【练习-1】(Uva 673) Parentheses Balance/平衡的括号 (栈stack)
- Penetration test (7) -- vulnerability scanning tool Nessus
- mysql导入数据库报错 [Err] 1273 – Unknown collation: ‘utf8mb4_0900_ai_ci’
猜你喜欢
渗透测试 ( 1 ) --- 必备 工具、导航
信息安全-安全编排自动化与响应 (SOAR) 技术解析
C language learning notes
Penetration testing (5) -- a collection of practical skills of scanning King nmap and penetration testing tools
Penetration test 2 --- XSS, CSRF, file upload, file inclusion, deserialization vulnerability
605. Planting flowers
Differential (one-dimensional, two-dimensional, three-dimensional) Blue Bridge Cup three body attack
Essai de pénétration (1) - - outils nécessaires, navigation
807. Maintain the urban skyline
Openwrt source code generation image
随机推荐
渗透测试 ( 2 ) --- 渗透测试系统、靶机、GoogleHacking、kali工具
【练习-11】4 Values whose Sum is 0(和为0的4个值)
Hdu-6025-prime sequence (girls' competition)
Penetration test (2) -- penetration test system, target, GoogleHacking, Kali tool
MySQL授予用户指定内容的操作权限
[exercise-7] crossover answers
Opencv learning log 31 -- background difference
[exercise-8] (UVA 246) 10-20-30== simulation
[exercise -10] unread messages
Basic Q & A of introductory C language
Flink 使用之 CEP
Auto.js入门
F - birthday cake (Shandong race)
628. Maximum product of three numbers
最全编程语言在线 API 文档
Analysis of protobuf format of real-time barrage and historical barrage at station B
Openwrt source code generation image
渗透测试 ( 8 ) --- Burp Suite Pro 官方文档
Research Report of cylindrical grinder industry - market status analysis and development prospect forecast
Truck History