二分是一种很有效的减少时间开销的策略, 我觉得单列出二分专题有些不太合理, 二分应该作为一中优化方法来考虑

这几道题都是简单的使用了二分方法优化, 二分虽然看似很简单, 但一不注意就会犯错. 在写二分时, 会遇到很多选择题, 很多"分叉路口", 要根据实际情况选择合适的"路"

HDU2199

Can you solve this equation?

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1195 Accepted Submission(s): 565
 
Problem Description
Now,given the equation 8*x^4 + 7*x^3 + 2*x^2 + 3*x + 6 == Y,can you find its solution between 0 and 100;
Now please try your lucky.
Input
The first line of the input contains an integer T(1<=T<=100) which means the number of test cases. Then T lines follow, each line has a real number Y (fabs(Y) <= 1e10);
Output

            For each test case, you should just output one real number(accurate up to 4 decimal places),which is the solution of the equation,or “No solution!”,if there is no solution for the equation between 0 and 100.
Sample Input
2
100
-4

Sample Output
1.6152
No solution!

Author
Redow
Recommend

注意看AC代码,循环的条件是 l+eps<r ,不能用 f(mid)==res 否则会一直循环下去(注意看循环内部, 有可能边界(l或r)就为正确的值, 这样的话永远不会搜索到)

代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<map>
#include<iomanip>
#define INF 0x7ffffff
#define MAXN 10000
using namespace std;
const double eps=1e-10;
double js(double x)
{return 8.0*x*x*x*x + 7.0*x*x*x + 2.0*x*x + 3.0*x + 6.0;
}
int main()
{#ifndef ONLINE_JUDGEfreopen("data.in", "r", stdin);#endifstd::ios::sync_with_stdio(false);std::cin.tie(0);double y;double x;double r,l;int t;cin>>t;while(t--){cin>>y;if(y<6||y>js(100)){cout<<"No solution!"<<endl;}else{l=0; r=100;double mid,res;while(l+eps<r){mid=(l+r)/2;if(y<js(mid)){r=mid;}else{l=mid;}}cout<<fixed<<setprecision(4)<<mid<<endl;}}return 0;
}

HDU2899

Strange fuction

 
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 666 Accepted Submission(s): 549
Problem Description
Now, here is a fuction:
  F(x) = 6 * x^7+8*x^6+7*x^3+5*x^2-y*x (0 <= x <=100)
Can you find the minimum value when x is between 0 and 100.
Input
The first line of the input contains an integer T(1<=T<=100) which means the number of test cases. Then T lines follow, each line has only one real numbers Y.(0 < Y <1e10)
Output

            Just the minimum value (accurate up to 4 decimal places),when x is between 0 and 100.
Sample Input
2
100
200

Sample Output
-74.4291
-178.8534

Author
Redow
 
Recommend
lcy

和上一道题一模一样,只不过这道题目拐了个弯, 需要进行求导

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<map>
#include<iomanip>
#define INF 0x7ffffff
#define MAXN 10000
using namespace std;
const double eps=1e-10;
double fd(double x)
{return 42*pow(x,6)+48*pow(x,5)+21*pow(x,2)+10*x;
}
int main()
{#ifndef ONLINE_JUDGEfreopen("data.in", "r", stdin);#endifstd::ios::sync_with_stdio(false);std::cin.tie(0);int t;double y;double l,r,mid;cin>>t;while(t--){cin>>y;l=0;r=100;while(r-l>eps){mid=(l+r)/2;if(fd(mid)>y){r=mid;}else l=mid;}cout<<fixed<<setprecision(4)<<6*pow(mid,7)+8*pow(mid,6)+7*pow(mid,3)+5*mid*mid-y*mid<<endl;}
}

HDU1969

Pie

 
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 939 Accepted Submission(s): 348
Problem Description
My birthday is coming up and traditionally I'm serving pie. Not just one pie, no, I have a number N of them, of various tastes and of various sizes. F of my friends are coming to my party and each of them gets a piece of pie. This should be one piece of one pie, not several small pieces since that looks messy. This piece can be one whole pie though.

My friends are very annoying and if one of them gets a bigger piece than the others, they start complaining. Therefore all of them should get equally sized (but not necessarily equally shaped) pieces, even if this leads to some pie getting spoiled (which is better than spoiling the party). Of course, I want a piece of pie for myself too, and that piece should also be of the same size.

What is the largest possible piece size all of us can get? All the pies are cylindrical in shape and they all have the same height 1, but the radii of the pies can be different.

Input
One line with a positive integer: the number of test cases. Then for each test case:
---One line with two integers N and F with 1 <= N, F <= 10 000: the number of pies and the number of friends.
---One line with N integers ri with 1 <= ri <= 10 000: the radii of the pies.
Output
For each test case, output one line with the largest possible volume V such that me and my friends can all get a pie piece of size V. The answer should be given as a floating point number with an absolute error of at most 10^(-3).
Sample Input
3
3 3
4 3 3
1 24
5
10 5
1 4 2 3 4 5 6 5 4 2

Sample Output
25.1327
3.1416
50.2655

 
Source
NWERC2006
Recommend
wangye

这道题没有固定的函数关系, 每一个待定的面积值都要去除每块pie的面积, 为了加快搜索到结果的速度, 可以采用二分的方法(可分成的块数大体上是相对于面积递增的)

代码

    #include <cstdio>  #include <cstring>  #include <algorithm>  #include <cmath>  using namespace std ;  #define PI 3.1415926535898  #define eqs 1e-5  double s[11000] ;  int n , m ;  double f(double x)  {  int k = (x+eqs) * 10000 ;  x = k * 1.0 / 10000 ;  return x ;  }  int solve(double x)  {  int i , j , num = 0 ;  for(i = n-1 ; i >= 0 && (s[i]-x) > eqs ; i--)  {  j = s[i] / x ;  num += j ;  if( num >= m+1 )return 1 ;  }  if( num >= m+1 ) return 1 ;  return 0 ;  }  int main()  {  int t , i , k ;  double low , mid , high , last ;  while( scanf("%d", &t) != EOF )  {  while(t--)  {  scanf("%d %d", &n, &m) ;  for(i = 0 ; i < n ; i++)  scanf("%lf", &s[i]) ;  sort(s,s+n) ;  for(i = 0 ; i < n ; i++)  s[i] = s[i]*s[i]*PI ;  low = 0 ;  high = s[n-1] ;  while( (high-low) > eqs )  {  mid = (low+high)/2.0 ;  if( solve(mid) )  {  low = mid  ;  last = mid ;  }  else  high = mid ;  }  printf("%.4lf\n", last) ;  }  }  return 0;  }

HDU2141

Can you find it?

 
Time Limit: 10000/3000 MS (Java/Others) Memory Limit: 32768/10000 K (Java/Others)
Total Submission(s): 1157 Accepted Submission(s): 373
Problem Description
Give you three sequences of numbers A, B, C, then we give you a number X. Now you need to calculate if you can find the three numbers Ai, Bj, Ck, which satisfy the formula Ai+Bj+Ck = X.
Input
There are many cases. Every data case is described as followed: In the first line there are three integers L, N, M, in the second line there are L integers represent the sequence A, in the third line there are N integers represent the sequences B, in the forth line there are M integers represent the sequence C. In the fifth line there is an integer S represents there are S integers X to be calculated. 1<=L, N, M<=500, 1<=S<=1000. all the integers are 32-integers.
Output
For each case, firstly you have to print the case number as the form "Case d:", then for the S queries, you calculate if the formula can be satisfied or not. If satisfied, you print "YES", otherwise print "NO".
Sample Input
3 3 3
1 2 3
1 2 3
1 2 3
3
1
4
10

Sample Output
Case 1:
NO
YES
NO

Author
wangye
Source
HDU 2007-11 Programming Contest
Recommend
威士忌

一样的题目, 不多说了

代码:

#include<stdio.h>
#include<algorithm>
using namespace std;
#define K 505
int LN[K*K];
int BinarySearch(int LN[],int h,int t)/*二分查找*/
{int left,right,mid;left=0;right=h-1;mid=(left+right)/2;while(left<=right){mid=(left+right)/2;if(LN[mid]==t)return 1;else if(LN[mid]>t)right=mid-1;else if(LN[mid]<t)left=mid+1;}return 0;
}
int main()
{int i,j,count=1,q;__int32 L[K],N[K],M[K],S,n,m,l;while(scanf("%d%d%d",&l,&n,&m)!=EOF){int h=0;for(i=0;i<l;i++)scanf("%d",&L[i]);for(i=0;i<n;i++)scanf("%d",&N[i]);for(i=0;i<m;i++)scanf("%d",&M[i]);for(i=0;i<l;i++)for(j=0;j<n;j++)LN[h++]=L[i]+N[j];/*合并L和N*/sort(LN,LN+h); /*对LN数组排序*/scanf("%d",&S);printf("Case %d:\n",count++);for(i=0;i<S;i++){scanf("%d",&q);/*q即为题目中的x*/int p=0; /*p为标记,0为找不到,1为能找到*/for(j=0;j<m;j++){int a=q-M[j]; /*因为L[i]+N[j]+M[k]==q,所以q-M[k]=LN[h]*/if(BinarySearch(LN,h,a)) /*在LN数组中查找到a*/{printf("YES\n");p=1;break;}}if(!p) /*找不到a*/printf("NO\n");}}return 0;
}

转载于:https://www.cnblogs.com/liuzhanshan/p/6052289.html

HDU2199,HDU2899,HDU1969,HDU2141--(简单二分)相关推荐

  1. 20行代码AC_ 习题8-1 Bin Packing UVA - 1149(贪心+简单二分解析)

    励志用少的代码做高效表达 题意 给定N个物品的中联L1,背包的容量M,同时要求每个背包最多装两个物品,求至少要多少个背包才能装下所有的物品. 解题过程 第一次接触背包类问题. 最初的思路是降序排序,i ...

  2. C.Defuse the Bombs(简单二分)

    C.Defuse the Bombs WA了九次,让我来看看简单二分有多简单 The terrorists have planted some bombs in a building! Our her ...

  3. 2020CCPC绵阳站 Defuse the Bombs(简单二分)

    2020CCPC绵阳站 Defuse the Bombs(简单二分) 题目 https://pintia.cn/problem-sets/1322796904464203776/problems/13 ...

  4. 2542 咖啡和作业 (简单二分)

    Coffee and Coursework 题意: 泰泰学长必须写他的课程作业.他的课程作业包含m 页. 为了提高效率,泰泰学长从某多多上买了n 杯咖啡. 对于第 i 杯咖啡泰泰学长可以摄 取 ai ...

  5. HDU2141(二分查找)

    题意:给出数据x,然后从a,b,c三个数组中找到满足关系a[i]+b[j]+c[k]=x的等式即可输出YES,否则输出NO.首先将a,b数组相加,然后合并的数组和c数组采用二分查找t=x-c[k]的值 ...

  6. POJ2239简单二分匹配

    题意:       一周有7天,每天可以上12节课,现在给你每科课的上课时间,问你一周最多可以上几科课,一科课只要上一节就行了. 思路:       简单题目,直接二分就行了,好久没写二分匹配了,练习 ...

  7. 洛谷【P1873】 砍树 简单二分解析

    题目链接:https://www.luogu.com.cn/problem/P1873 题目描述: 伐木工人米尔科需要砍倒 M 米长的木材.这是一个对米尔科来说很容易的工作,因为他有一个漂亮的新伐木机 ...

  8. POJ2446 模板盖格子 简单二分匹配

    题意:       给你一个n*m的格子,有的格子上有坑,然后让你用1*2的东西去覆盖所有没有坑的格子,不能重叠,坑上也不能放东西覆盖,问是否能成功. 思路:        简单题目,每个格子和四周的 ...

  9. P1873 砍树(简单二分)

    P1873 砍树 题目描述 伐木工人米尔科需要砍倒M米长的木材.这是一个对米尔科来说很容易的工作,因为他有一个漂亮的新伐木机,可以像野火一样砍倒森林.不过,米尔科只被允许砍倒单行树木. 米尔科的伐木机 ...

最新文章

  1. Java方向如何准备BAT技术面试答案
  2. jQuery知识汇总
  3. NYOJ练习题 又见Alice and Bob
  4. unixODBC的使用
  5. 李名洋(1983-),男,中国电信股份有限公司云计算分公司数据分析师。
  6. idea中查看maven信息
  7. 院士大咖齐聚苏州,共话AI未来,30个优秀个人和企业、产品受到表彰
  8. CF 316G3 Good Substrings——广义后缀自动机
  9. Power bi_商品销售案例分析
  10. NTFS与FAT 32的区别
  11. xp计算机管理员桌面文件没有权限,电脑中右击文件没有以管理员身份运行选项怎么办...
  12. L1-054 福到了 (15 分)
  13. sysvol 域控制器 文件_重建域控SYSVOL和NETLOGON共享
  14. vue-router 快速返回上一页
  15. Ae 入门系列之二:素材的导入与管理
  16. 极性表面积与非极性表面积
  17. SIL软件在环学习笔记
  18. java连接.accdb_尝试通过Java 8中的JDBC-ODBC连接到.accdb文件时...
  19. PHP开发环境MySQL下载安装及配置流程
  20. 第三篇:设计模式六大原则: 一个萝卜一个坑 -- 单一职责原则

热门文章

  1. npm run dev 出现npm ERR!missing scrip:dev
  2. 当你收到面试通知后,如下的准备可以大大提升面试成功率
  3. Mysql高性能优化规范建议,太厉害了!
  4. 【docker】docker run命令详解
  5. 【Scratch】青少年蓝桥杯_每日一题_4.19_考试成绩
  6. C语言 编写程序:请将Fibonacci数列前30项中的偶数值找出来,存储到一维数组中。其中,Fibonacci数列如下:1,1,2,3,5,8,13,21,34...该数列除前两项之外,其他任意
  7. matlab计算原点矩,关于用matlab求样本均值方差以及k阶原点矩的matlab程序
  8. 计算机二级c语言可以提前交卷吗,软考考试中可以提前交卷吗?计算机软考成绩一般多久公布...
  9. mysql 硬盘空间不够_mysql磁盘空间不足的查排
  10. 初中计算机基础知识说课稿,计算机基础知识说课稿