zoukankan      html  css  js  c++  java
  • POJ 3579 Median (二分)

                                                                                                         Median
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 7423   Accepted: 2538

    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

    题意:一个序列  他们的差值的绝对值产生一个新的序列  问这个序列的中位数是那个
    二分答案 
    判断的地方我们也需要用二分 假如 中位数是x的话  我们判断最开始的那个数组a
    假如我们知道a[i]+x在a数组的位置 那么我就知道 和a[i]的差值小于等于x的个数为n-t个 我们就可以知道有多少个数是小于这个x的 
     
     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstdlib>
     4 #include<cctype>
     5 #include<cmath>
     6 #include<cstring>
     7 #include<map>
     8 #include<stack>
     9 #include<set>
    10 #include<vector>
    11 #include<algorithm>
    12 #include<string.h>
    13 typedef long long ll;
    14 typedef unsigned long long LL;
    15 using namespace std;
    16 const int INF=0x3f3f3f3f;
    17 const double eps=0.0000000001;
    18 const int N=100000+10;
    19 int a[N];int n,m;
    20 int judge(int x){
    21     int sum=0;
    22     for(int i=0;i<n;i++){
    23         int t=lower_bound(a,a+n,a[i]+x)-a;
    24         // 存在差值 x
    25         sum=sum+n-t;// 和a[i]的差值小于等于x的个数为n-t;
    26     }
    27     if(sum>m)return 1;
    28     else{
    29         return 0;
    30     }
    31 }
    32 int main(){
    33 
    34     while(scanf("%d",&n)!=EOF){
    35         int maxx=0;
    36         for(int i=0;i<n;i++){
    37             scanf("%d",&a[i]);
    38         }
    39         sort(a,a+n);
    40         m=n*(n-1)/4;
    41         int low=0;
    42         int ans;
    43         int high=a[n-1];
    44         while(low<=high){
    45             int mid=(low+high)>>1;
    46             if(judge(mid)){
    47                 ans=mid;
    48                 low=mid+1;
    49             }
    50             else
    51                 high=mid-1;
    52         }
    53         cout<<ans<<endl;
    54     }
    55 }
  • 相关阅读:
    动态规划 最长公共子序列 LCS,最长单独递增子序列,最长公共子串
    梳排序(Comb sort)
    地精排序(Gnome Sort) 算法
    vs2010 调试 调用堆栈 窗口
    vs2010 条件断点 has changed是什么意思?
    vs2010根据字符串内容添加断点
    vs2010 调试中监视变量
    vs2010断点使用技巧
    区间重合判断(pojg校门外的树)
    转:Linus:利用二级指针删除单向链表
  • 原文地址:https://www.cnblogs.com/Aa1039510121/p/6903345.html
Copyright © 2011-2022 走看看