日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Median(二分+二分)

發布時間:2025/3/15 编程问答 17 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Median(二分+二分) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Median

?http://poj.org/problem?id=3579

Time Limit:?1000MS?Memory Limit:?65536K
Total Submissions:?11225?Accepted:?4016

Description

Given?N?numbers,?X1,?X2, ... ,?XN, let us calculate the difference of every pair of numbers: ∣Xi?-?Xj∣ (1 ≤?i?<?j?≤?N). We can get?C(N,2)?differences through this work, and now your task is to find the median of the differences as quickly as you can!

Note in this problem, the median is defined as the?(m/2)-th? smallest number if?m,the amount of the differences, is even. For example, you have to find the third smallest one in the case of?m?= 6.

Input

The input consists of several test cases.
In each test case,?N?will be given in the first line. Then?N?numbers are given, representing?X1,?X2, ... ,?XN, (?Xi?≤ 1,000,000,000? 3 ≤ N ≤ 1,00,000 )

Output

For each test case, output the median in a separate line.

Sample Input

4 1 3 2 4 3 1 10 2

Sample Output

1 8

Source

POJ Founder Monthly Contest – 2008.04.13, Lei Tao

?

1 #include<iostream> 2 #include<algorithm> 3 #include<string> 4 #include<map> 5 #include<vector> 6 #include<cmath> 7 #include<string.h> 8 #include<stdlib.h> 9 #include<stack> 10 #include<queue> 11 #include<cstdio> 12 #define ll long long 13 const long long MOD=1000000007; 14 #define maxn 100005 15 using namespace std; 16 17 int n; 18 int m; 19 int a[maxn]; 20 21 bool Check(int mid){ 22 int sum=0; 23 int p; 24 for(int i=1;i<=n;i++){ 25 p=upper_bound(a+1,a+n+1,a[i]+mid)-a; 26 sum+=p-i-1;//排除a[i]之前的那些元素,共有i+1; 27 } 28 if(sum>=m){ 29 return true; 30 } 31 return false; 32 } 33 34 int main(){ 35 while(~scanf("%d",&n)){ 36 m=n*(n-1)/2; 37 m=(m+1)/2; 38 for(int i=1;i<=n;i++){ 39 scanf("%d",&a[i]); 40 } 41 sort(a+1,a+n+1); 42 ll L=0,R=1000000000,mid; 43 while(L<=R){ 44 mid=L+R>>1; 45 if(Check(mid)){ 46 R=mid-1; 47 } 48 else{ 49 L=mid+1; 50 } 51 } 52 printf("%d\n",L); 53 } 54 55 } View Code

?

轉載于:https://www.cnblogs.com/Fighting-sh/p/10054652.html

總結

以上是生活随笔為你收集整理的Median(二分+二分)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。