麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 學院 > 開發設計 > 正文

POJ 3579 Median(2次二分)

2019-11-10 20:22:15
字體:
來源:轉載
供稿:網友

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 ofm = 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, representingX1, 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
41 3 2 431 10 2Sample Output
18

  思路:本題直接使用暴力法,時間復雜度約為n^2,超時,所以采用二分法。

  先把差值分出來,再用二分法驗證差值是否符合要求。

#include<algorithm>#include<cstdio>#include<cstdlib>using namespace std;int n,m;int str[100005];int judge(int mid){   int cnt=0;    for(int i=0;i<n;i++)    {        cnt+=n-(lower_bound(str,str+n,str[i]+mid)-str);//C++中STL的查找函數
    }    return cnt>m?1:0;}int main(){   //freopen("e://in.txt","r",stdin);    while(scanf("%d",&n)==1)    {     m=n*(n-1)/4;         for(int i=0;i<n;i++)          scanf("%d",&str[i]);          sort(str,str+n);          int left=0,right=str[n-1]+str[0],mid;          while(left<=right)          {              mid=(left+right)/2;              if(judge(mid))                left=mid+1;              else                right=mid-1;          }          printf("%d/n",left-1);    }    return 0;}

總結:lower_bound函數的引用

 


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 国产喷白浆10p | 在线1区| chinese乱子伦xxxx国语对白 | 免费视频a | 特片网久久 | 久久色播 | 特片网久久 | 高清做爰免费无遮网站挡 | jizzyouxxxx| 日本一道aⅴ不卡免费播放 视屏一区 | 久久亚洲精品国产 | 中文字幕精品一区久久久久 | 蜜桃网站在线观看 | 黄色网址在线免费 | 在线成人免费网站 | 亚洲少妇诱惑 | 一级做a爱视频 | 91 视频网站 | 人人做人人看 | 国产精品色在线网站 | 久久伊人国产精品 | 国产午夜精品视频免费不卡69堂 | 国产成人精品自拍视频 | 久久久久夜色精品国产老牛91 | 97伦理 | 精品国产一区在线 | 精品国产一区二区三区在线观看 | 午夜丰满少妇高清毛片1000部 | 水卜樱一区二区av | 毛片在哪里看 | 黄色特级视频 | 91九色电影 | 奶子吧naiziba.cc免费午夜片在线观看 | 极品销魂一区二区三区 | 久久里面有精品 | 亚洲小视频在线 | www日韩大片 | 久久精品日产第一区二区三区 | 精品一区二区三区免费看 | 久久成年网站 | 亚洲国产成人一区 |