答案二分,lowerbound,upperbound

来源:互联网 发布:欧洲旅游价格知乎 编辑:程序博客网 时间:2024/05/20 18:02
#include<iostream>#include<algorithm>#include<cstdio>using namespace std;typedef long long LL;const int maxn = (int) 1e5 + 7;int A[maxn];int d;基本思路:由于绝对值在0到max(A[i])(i>=0,i<n)中所以只要在0-1000000000之间二分=30次就可以找到答案二分的判断是比较所有情况中的小于目前举出的中位数的个数这个类举得复杂度为n*logn=17*100000/2bool check(int x,int n){    LL cnt = 0;    for (int i = 0; i < n; i++)    {        cnt += n - (lower_bound(A + i, A + n, x + A[i]) - (A));    }    return cnt <= d;}int main(){#ifndef Local        freopen("in.txt", "r", stdin);#endif    int n;    while (cin >> n)    {        for (int i = 0; i<n; i++)        {            scanf("%d", A+i);        }        sort(A,A+n);        d = n*(n - 1) / 2;        if (d % 2)        {            d = d >> 1;        }        else        {            d = (d + 1) >> 1;        }        int left = A[0],right=A[n-1]-A[0]+1;        while (left < right)        {            int mid = (left + right) >> 1;        }    }    return 0;}
0 0
原创粉丝点击