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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

poj 3579 Median 中间值(二分搜索)

發布時間:2025/4/16 编程问答 12 豆豆
生活随笔 收集整理的這篇文章主要介紹了 poj 3579 Median 中间值(二分搜索) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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 ifm,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

#include <iostream> #include <cstdio> #include <algorithm> #define INF 1000000 using namespace std; int main() {int n;int a[100000];while(cin>>n){for(int i=0;i<n;++i)scanf("%d",&a[i]);int m,temp=n*(n-1)/2;if(temp&1)m=temp/2+1;elsem=temp/2; //有n個數,按照順序排列,涼涼相減,則有n-1個數,問這n-1個數的中間值所在的位置(1......n-1排列中的位 置)m,其1、2....m的和為多少?sort(a,a+n);int low=0,high=a[n-1]-a[0];int mid,value;while(high>=low)//{int ans=0;mid=low+(high-low)/2;int pos;for(int i=0;i<n-1;++i){pos=upper_bound(a+i,a+n,mid+a[i])-a;//mid(兩數之差,設定為中間值)則a[i]+mid為數組a中的中間值,pos為中間值在數組中的位置。//ans+=pos-i-1;}if(ans>=m){value=mid;high=mid-1;}elselow=mid+1;}cout<<high+1<<endl;} }

總結

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

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