C. Binary Search
Posted 俄罗斯刺沙蓬
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C. Binary Search相关的知识,希望对你有一定的参考价值。
题目
题意
- 给一个数字n,构造出一个全排列的数组a,满足上面二分结果为true
- 请求出不同全排列数组a的数量,答案模1e9+7
思路
- 模拟:按照二叉查找树的思路,模拟这个二分所有可能遇到的mid,使得判断条件成立(为什么落在最后的点上?因为是折半查找,搜索树上没有重复的节点)
- 这样得到了必须大于等于x位置的数量,和必须小于x位置的数量,也就是知道了剩下没有遇到的位置该如何排列(全排列)
ans = C[Cles][les.size()] * fact[les.size()] % mod;
计算小于的数量(特殊处理遇到pos节点的位置,即忽略)
ans = (ans * (C[Cgre][gre.size()] * fact[gre.size()] % mod)) % mod;
计算大于的数量
ans = (ans * fact[n - les.size() - gre.size() - 1]) % mod;
计算剩余的排列数量- 需要用到组合数和计数原理,和二分
代码
const double PI = acos(-1.0);
const int N = 1010, mod = 1e9 + 7;
int C[N][N];
int fact[N];
void solve()
int n, x, pos;
cin >> n >> x >> pos;
vector<int> gre, les;
vector<int> idx;
int l = 0, r = n;
while(l < r)
int mid = l + r >> 1;
idx.push_back(mid);
if(mid <= pos)
l = mid + 1;
if(mid != pos)les.push_back(mid);
else
r = mid;
gre.push_back(mid);
// debug1(mid);
int Cles = x - 1;
int Cgre = n - x;
// debug2(Cles, Cgre);
// debug2(les.size(), gre.size());
int ans = C[Cles][les.size()] * fact[les.size()] % mod;
ans = (ans * (C[Cgre][gre.size()] * fact[gre.size()] % mod)) % mod;
ans = (ans * fact[n - les.size() - gre.size() - 1]) % mod;
cout << ans << endl;
signed main()
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
C[0][0] = fact[0] = 1;
for (int i = 1; i <= 1000;i ++)
C[i][0] = C[i][i] = 1;
fact[i] = (fact[i - 1] * i) % mod;
for (int j = 1; j < i;j ++)
C[i][j] = (C[i - 1][j] + C[i - 1][j - 1]) % mod;
// caseT
solve();
return 0;
475. Heaters (start binary search, appplication for binary search)
Winter is coming! Your first job during the contest is to design a standard heater with fixed warm radius to warm all the houses. Now, you are given positions of houses and heaters on a horizontal line, find out minimum radius of heaters so that all houses could be covered by those heaters. So, your input will be the positions of houses and heaters seperately, and your expected output will be the minimum radius standard of heaters. Note: Numbers of houses and heaters you are given are non-negative and will not exceed 25000. Positions of houses and heaters you are given are non-negative and will not exceed 10^9. As long as a house is in the heaters‘ warm radius range, it can be warmed. All the heaters follow your radius standard and the warm radius will the same. Example 1: Input: [1,2,3],[2] Output: 1 Explanation: The only heater was placed in the position 2, and if we use the radius 1 standard, then all the houses can be warmed. Example 2: Input: [1,2,3,4],[1,4] Output: 1 Explanation: The two heater was placed in the position 1 and 4. We need to use radius 1 standard, then all the houses can be warmed.
Solution: why we use binary search here, thr brute force search method is get max(min (dist)), to reduce time complexixity we need to use binary search(why), there are two elments(in heaters) close to the houses[i]
Here binary search model is to find first >= target. (basic model is 35 search insert poition)
class Solution { //check houses, compare maxdist(min(house[i], heaters[j]) : min distance between house[i], heaters[j]; and get max of them; max(min(dist)) public int findRadius(int[] houses, int[] heaters) { int radius = 0;//max Arrays.sort(heaters); for(int i = 0; i<houses.length; i++){ int min = Integer.MAX_VALUE; //binary search (find first >= houses), target is houses[i] int l = 0, r = heaters.length-1; while(l <= r){ int m = (r-l)/2 + l; if(heaters[m] >= houses[i]) r = m-1 ; else l = m+1; } //System.out.println(l); //l is the index, could be 0, >=heaters.length int d1 = l-1>=0 ? (houses[i] - heaters[l-1]) : Integer.MAX_VALUE; int d2 = l<heaters.length ? (heaters[l] - houses[i]): Integer.MAX_VALUE;// handle all the things min = d1<d2 ? d1 : d2; if(min > radius) radius = min; } return radius; } }
Another way : call built in function of binary search in the Arrays.binarySearch(); (https://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html#binarySearch(int[],%20int)) https://www.geeksforgeeks.org/arrays-binarysearch-java-examples-set-1/(geekforgeek)
public class Solution { public int findRadius(int[] houses, int[] heaters) { Arrays.sort(heaters); int result = Integer.MIN_VALUE; for (int house : houses) { int index = Arrays.binarySearch(heaters, house); if (index < 0) { index = -(index + 1); } int dist1 = index - 1 >= 0 ? house - heaters[index - 1] : Integer.MAX_VALUE; int dist2 = index < heaters.length ? heaters[index] - house : Integer.MAX_VALUE; result = Math.max(result, Math.min(dist1, dist2)); } return result; } }
lastly, remember to sort first
以上是关于C. Binary Search的主要内容,如果未能解决你的问题,请参考以下文章
475. Heaters (start binary search, appplication for binary search)
[Leetcode] Binary search tree --Binary Search Tree Iterator
[Lintcode]95. Validate Binary Search Tree/[Leetcode]98. Validate Binary Search Tree
[LeetCode] 173. Binary Search Tree Iterator_Medium_tag: Binary Search Tree