zoukankan      html  css  js  c++  java
  • 二分模板// The Frog's Games +Aggressive cows

      二分的情况不同所输出的模板也不同列如下面两个题目

                                                                                                 The Frog's Games  

    代码如下

                   

                                                                   

                                                        //二分专练
                                                    #include <stdio.h> //lower_bound大于等于它的第一个数
                                                    #include <iostream> //upper_bound大于 它的第一个数
                                                    #include <algorithm>
                                                    #include <string.h>
                                                    using namespace std;
                                                    int a[500005];
                                                    int n,m;//l河长,n块石头位子,可以跳m次
                                                    bool lpt(int wf)
                                                    {
                                                          int pos=0;
                                                    for(int x=1;x<=m;x++)
                                                    {
                                                          pos=upper_bound(a,a+n,a[pos]+wf)-a-1;
                                                         if(pos>=n-1)
                                                         return true;
                                                    }
                                                          return false;
                                                   }
                                                   int main()
                                                   {
                                                         int l,i,j,left,right,mid;
                                                      while(scanf("%d%d%d",&l,&n,&m)!=EOF)
                                                   {
                                                         int minn=1e9;
                                                         for(i=1;i<=n;i++)
                                                         scanf("%d",&a[i]);
                                                         sort(a,a+n+1);
                                                         a[0]=0;a[n+1]=l;
                                                         n=n+2;
                                                         right=l;
                                                         left=l/m;
                                                       while(left<=right)
                                                   {
                                                          mid=(left+right)/2;
                                                           if(lpt(mid))
                                                      {
                                                               right=mid-1;
                                                               minn=min(minn,mid);
                                                      }
                                                           else
                                                         left=mid+1;
                                                  }
                                                         printf("%d ",minn);
                                                  }
                                                         return 0;

                                                }
                                                                                                                 Aggressive cows   

                                       代码如下

                                         

                                           //二分专练
                                           #include <stdio.h> //lower_bound大于等于它的第一个数
                                           #include <iostream> //upper_bound大于 它的第一个数
                                           #include <algorithm>
                                           #include <string.h>
                                           using namespace std;
                                           int a[100005];
                                           int n,c;
                                           bool lpt(int x)
                                          {
                                             int d,j,ans=0; d=a[0];
                                              for(j=1;j<=n-1;j++)
                                            {
                                               if(a[j]-d>=x)
                                              {
                                                 ans++;
                                                 d=a[j];
                                              }
                                            }
                                              if(ans<c-1) return true;
                                                        else return false;
                                         }
                                         int main()
                                        {
                                            int maxm=0,i,m,mid,left,right;
                                            scanf("%d%d",&n,&c);
                                            for(i=0;i<n;i++)
                                            scanf("%d",&a[i]);
                                            sort(a,a+n);//1 2 4 8 9
                                            left=0;
                                            right=(a[n-1]-a[0])/(c-1);//4
                                           while(left<=right)
                                          {
                                              mid=(left+right)/2;
                                             if(lpt(mid))
                                            {
                                              right=mid-1;
                                            }
                                              else
                                           {
                                               left=mid+1;
                                               maxm=max(maxm,mid);
                                            }
                                          }
                                           printf("%d ",maxm);
                                          return 0;
                                       }

                                   c语言代码~ 初学者一起来学、、

  • 相关阅读:
    mysql一个字段多个值如何分别取出这些值
    mysql json数组取值、查询
    mui删除元素
    软件测试的艺术(读书笔记1)
    爬虫(3)_网站分析
    谈谈作为测试的8年
    测试有前景吗?
    爬虫(二)-创建项目&应用
    爬虫(一)-环境搭建
    puppeteer UI自动化测试demo(一)
  • 原文地址:https://www.cnblogs.com/lptlptlpt/p/11830128.html
Copyright © 2011-2022 走看看