您的位置:首页 > 其它

答案二分,lowerbound,upperbound

2017-03-05 12:41 169 查看
#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/2

bool 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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: