#include<iostream> usingnamespace std; constint MAXN = 1e6 + 7; int num[MAXN]; int n; intbinary(int x) { int ans = -1;//-1说明ans不存在 int left = 0, right = n - 1; int mid;
while (left <= right) { mid = (left + right) / 2; if (num[mid] >= x) {
ans = num[mid]; right = mid - 1; } else left = mid + 1; } return ans; }
intmain() { int x; cin >> n >> x; for (int i = 0; i < n; i++) { scanf("%d", &num[i]); }
intbinaryindex(int x) { int ans = -1; int left = 0, right = n - 1; int mid;
while (left <= right) { mid = (left + right) / 2; if (num[mid] >= x) {
ans =mid; right = mid - 1; } else left = mid + 1; } return ans; } intmain() { int x; cin >> n >> x; for (int i = 0; i < n; i++) { scanf("%d", &num[i]); }
题目描述 Farmer John 建造了一个有 N(2<=N<=1000000)个隔间的牛棚,这些隔间分布在一条直线上,坐标是 x1x_1x1 ,…,xNx_NxN(0<xi<1000000000),他的c(2<c<n)头牛不满于隔间的位置分布,它们为牛棚里其他的牛的存在而愤怒。为了防止牛之间的互相打斗,Farmer John 想把这些牛安置在指定的隔间,所有牛中相邻两头的最近距离越大越好。那么,这个最大的最近距离是多少呢?
intcoutcow(int dis) { int now = arr[0]; int i = 1; int cnt = 1; for (i = 1; i < N; i++) { if (arr[i] - now >= dis) { cnt++; now = arr[i]; } } return cnt; }
boolbeyondC(int dis) { returncoutcow(dis) >= C; }
intbinary() { int left = 1, right = 1e9; int ans = -1; int mid; while (left <= right) { mid = (left + right) / 2; if (beyondC(mid)) { ans = mid; left = mid + 1; } else right = mid - 1; } return ans; }
intmain() { cin >> N >> C; int i = 0; for (i = 0; i < N; i++) { scanf("%d", &arr[i]); } qsort(arr, N, sizeof(int), comp); cout << binary() << endl; return0; }
#include<iostream> usingnamespace std; constint MAXN = 1e6 + 7; int arr[MAXN]; int n, m;
intcount(int b) { int sum = 0; int i = 0; for (i = 0; i < n; i++) { sum += arr[i] / b;//sum计算在分b个糖果的情况下可有多少个人
} return sum; }
boolcheck(int b,int m) { returncount(b) >= m; }
intbinary(int& n, int& m) { int left = 1, right = 1e6; int mid; int ans = 1; while (left <= right) { mid = (left + right) / 2; if (check(mid,m)) { ans = mid; left = mid + 1; } else { right = mid - 1; }
} return ans; }
intmain() { //n种口味,m个人 int t; cin >> t ; while (t--) { cin >> n >> m; int i = 0; for (i = 0; i < n; i++) { scanf("%d", &arr[i]); }
int min = arr[0]; cout<<binary( n, m)<<endl; } return0; }