• 蓝桥杯刷题6--二分法


    1. 二分法

    注意二分法的应用条件是:序列是单调有序的,从小到大,或从大到小。在无序的序列上无法二分,如果是乱序的,应该先排序再二分。

    1.1 代码实现

    1. import java.util.*;
    2. class Main {
    3. static int[] a = new int[105];
    4. static boolean check(int x, int mid) { return x <= a[mid]; }
    5. static int bin_search(int n, int x) {
    6. int L = 1;
    7. int R = n;
    8. while (L < R) {
    9. int mid = (L + R) / 2;
    10. if (check(x, mid)) R = mid;
    11. else L = mid + 1;
    12. }
    13. return a[L];
    14. }
    15. public static void main(String[] args) {
    16. int n = 100;
    17. for (int i = 1; i <= n; i++) a[i] = i;
    18. int x = 68;
    19. System.out.println("x=" + bin_search(n, x));
    20. }
    21. }

    2. 习题

    2.1 求阶乘

    1. import java.util.*;
    2. public class Main {
    3. public static long check(long mid){
    4. long count = 0;
    5. while(mid > 0){
    6. count += mid/5;
    7. mid /= 5;
    8. }
    9. return count;
    10. }
    11. public static void main(String[] args) {
    12. Scanner scan = new Scanner(System.in);
    13. long k = scan.nextLong();
    14. long left = 0;
    15. long right = (long) Math.pow(10,19);
    16. while(left < right){
    17. long mid = (left + right)/2;
    18. if(check(mid) >= k) right = mid;
    19. else left = mid + 1;
    20. }
    21. if(check(right)==k) System.out.println(right);
    22. else System.out.println(-1);
    23. scan.close();
    24. }
    25. }

    2.2 分巧克力

    1. import java.util.*;
    2. public class Main {
    3. static int k,n;
    4. static final int max = 100010;
    5. static int h[] = new int[max];
    6. static int w[] = new int[max];
    7. public static void main(String[] args) {
    8. Scanner scan = new Scanner(System.in);
    9. n = scan.nextInt();
    10. k = scan.nextInt();
    11. for(int i = 0;i
    12. h[i] = scan.nextInt();
    13. w[i] = scan.nextInt();
    14. }
    15. int L = 1;
    16. int R = max;
    17. while(L
    18. int mid = (L + R + 1) >> 1;
    19. if(check(mid)) L = mid;
    20. else R = mid-1;
    21. }
    22. System.out.println(L);
    23. scan.close();
    24. }
    25. public static boolean check(int d){
    26. int count = 0;
    27. for(int i = 0;i
    28. count += (h[i]/d) * (w[i]/d);
    29. }
    30. if(count >= k) return true;
    31. else return false;
    32. }
    33. }

    2.3 最小值最大化:跳石头

    1. import java.util.Scanner;
    2. public class Main {
    3. static int len, n, m;
    4. static int[] stone;
    5. static boolean check(int d) {
    6. int num = 0; // num记录搬走石头的数量
    7. int pos = 0; // 当前站立的石头
    8. for (int i = 1; i <= n; ++i)
    9. if (stone[i] - pos < d) num++; // 可以搬走
    10. else pos = stone[i]; // 不能搬走
    11. if (num <= m) return true;
    12. else return false;
    13. }
    14. public static void main(String[] args) {
    15. Scanner scan = new Scanner(System.in);
    16. len = scan.nextInt();
    17. n = scan.nextInt();
    18. m = scan.nextInt();
    19. stone = new int[n + 1];
    20. for (int i = 1; i <= n; i++){
    21. stone[i] = scan.nextInt();
    22. }
    23. int L = 0;
    24. int R = len;
    25. while (L < R) {
    26. int mid = L + (R - L) / 2;
    27. if (check(mid)) L = mid + 1; // 说明mid小了,调大一点
    28. else R = mid - 1; // 说明mid大了,调小一点
    29. }
    30. if (!check(R)) R += 1;
    31. System.out.println(R);
    32. scan.close();
    33. }
    34. }

    2.4 青蛙过河

    1. import java.util.*;
    2. public class Main{
    3. public static void main(String[] args) {
    4. Scanner scan = new Scanner(System.in);
    5. int n =scan.nextInt();
    6. int x = scan.nextInt();
    7. long[] arr = new long[n+1];//存放每一个石头的位置
    8. for(int i=1;i// * 0,1,1,2,2,inf
    9. arr[i] = scan.nextLong()+arr[i-1];
    10. }
    11. arr[n] = arr[n-1]+1<<10;//对岸设置为无穷大
    12. int l=0;
    13. int r = n;
    14. while(l
    15. int mid = (l+r)/2;
    16. if(check(mid,arr,n,x)) {
    17. r = mid;
    18. }else {
    19. l = mid+1;
    20. }
    21. }
    22. System.out.println(l);
    23. }
    24. public static boolean check(int k,long[] arr,int n,int x) {
    25. for(int i=0;i//任意k个大小的区间大于等于2*x
    26. if(arr[i+k]-arr[i] <2*x)
    27. return false;
    28. }
    29. return true;
    30. }
    31. }
  • 相关阅读:
    VR开发(一)——SteamVR实现摇杆移动
    2023 家电行业品牌社媒营销洞察报告
    Vue 3 组合式编程:革新前端开发的新时代
    JAVA jdk8安装
    分类算法系列⑥:随机森林
    爬虫(9) - Scrapy框架(1) | Scrapy 异步网络爬虫框架
    SAP系统里的统驭科目
    AI角色对环境信息的感知方式
    Visual Studio 代码显示空格等空白符
    JVM篇---第十一篇
  • 原文地址:https://blog.csdn.net/weixin_72151610/article/details/136374195