Light Bulb ZOJ - 3203 三分
三分:
和二分非常类似的一个算法,与二分不同的是
二分是单调的,而三分是一个先增后减或者先减后增
三分可以求出峰值。
注意三分一定是严格单调的,不能有相等的情况。
讲个例题:
题意:
一个人发现他的影子的长度随着他在灯泡和墙壁之间走到时发生着变化。一个突然的想法出现在脑海里,他想知道他的影子的最大长度。
例图:
输入格式
输入文件的第一行包含一个整数 ,表示测试数据的组数。
对于每组测试数据,仅一行,包含三个实数 , 和 , 表示灯泡的高度, 表示 mildleopard 的身高, 表示灯泡和墙的水平距离。
输出格式
输出文件共 行,每组数据占一行表示影子的最大长度,保留三位小数。
样例
样例输入
3
2 1 0.5
2 0.5 3
4 3 4
样例输出
1.000
0.750
4.000
题解:
1 #include<stdio.h>
2 #include<stdlib.h>
3 #include<iostream>
4 #include<string.h>
5 #include<algorithm>
6 using namespace std;
7 const int maxn=500005;
8 const int INF=0x3f3f3f3f;
9 const int eps=1e-11;
10 typedef long long ll;
11 double height,high,width;
12 double panduan(double x)
13 {
14 double ans=high/height*width;
15 double dis=width-x; //dis就是人距离墙的距离
16 if(ans-dis>0) //ans就是影子不在墙的上面时它的长度
17 {
18 double z=dis/width;
19 double sum=dis+(z*height-high)/(z-1.0);
20 return sum;
21 }
22 else
23 {
24 return ans;
25 }
26 }
27 int main()
28 {
29 int t;
30 scanf("%d",&t);
31 while(t--)
32 {
33 scanf("%lf%lf%lf",&height,&high,&width);
34 double l=0,r=width; //我的三分是三分的人距离墙的距离
35 while(r - l > 1e-11) //这就是三分模板了,只要确定了三分的上界和下界就可以直接代入使用
36 {
37 double m1 = l + (r - l) / 3;
38 double m2 = r - (r - l) / 3;
39 if(panduan(m1) > panduan(m2)) r = m2;
40 else l = m1;
41 }
42 printf("%.3lf\n", panduan(l));
43 }
44 return 0;
45 }
Light Bulb ZOJ - 3203 三分的更多相关文章
- ZOJ 3203 Light Bulb (三分+计算几何)
B - Light Bulb Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu Submit ...
- 三分 --- ZOJ 3203 Light Bulb
Light Bulb Problem's Link: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3203 Mean: ...
- ZOJ 3203 Light Bulb (三分查找)
Light Bulb Time Limit: 1 Second Memory Limit: 32768 KB Compared to wildleopard's wealthiness, h ...
- ZOJ 3203 Light Bulb - 求导求最大值
如果L全在地面上: 输出 h * D / H 如果L全在墙上: 输出 h 否则: (D - X ) / X = Y / (H - h) L = D - X + h - Y 然后对L求导即可 #incl ...
- zoj 3203 Light Bulb,三分之二的基本问题
Light Bulb Time Limit: 1 Second Memory Limit: 32768 KB Compared to wildleopard's wealthiness, h ...
- ZOJ 3203 Light Bulb
Compared to wildleopard's wealthiness, his brother mildleopard is rather poor. His house is narrow a ...
- ZOJ 3203 Light Bulb(数学对勾函数)
Light Bulb Time Limit: 1 Second Memory Limit: 32768 KB Compared to wildleopard's wealthiness, h ...
- [清华集训2015]灯泡(浙江大学ZOJ 3203 Light Bulb)
Time Limit: 1 Second Memory Limit: 32768 KB Compared to wildleopard's wealthiness, his brother ...
- Light Bulb(三分)
ZOJ Problem Set - 3203 Light Bulb Time Limit: 1 Second Memory Limit: 32768 KB Compared to wildl ...
随机推荐
- selenium自动化 | 借助百度AI开放平台识别验证码登录职教云
#通过借助百度AI开放平台识别验证码登录职教云 from PIL import Image from aip import AipOcr import unittest # driver.get(zj ...
- leetcode230. 二叉搜索树中第K小的元素
题目链接: https://leetcode-cn.com/problems/kth-smallest-element-in-a-bst/ 题目: 给定一个二叉搜索树,编写一个函数 kthSmalle ...
- cursor pin s和cursor pin s wait on x
1.cursor pin s是一个共享锁,一般情况下是因为发生在SQL短时间内大量执行 案例:在生产库中,突然出现大量的cursor pin s的等待,询问是否有动作后,同事说有编译存储过程(被误导了 ...
- Spring-AOP为类增加新的功能
适配器的简单应用实现: 比如:有一个类Car,在类中有两个属性一个为汽车名name,另一个为速度speed.其行为为run(). 现在有一辆车BMWCar 增加了GPS功能.如下实现: 基本类: pu ...
- 入门OJ:扫雪
扫雪1 题目描述 大雪履盖了整个城市,市政府要求冬季服务部门尽快将一些街道(列在一份清单中)的积雪清除掉以恢复交通,整个城市由许多交叉路口和街道构成,当然任意两个交叉路口都是直接或间接连通的,清单给出 ...
- Java并发编程常识
这是why的第 85 篇原创文章 写中间件经常要做两件事: 1.延迟加载,在内存缓存已加载项. 2.统计调用次数,拦截并发量. 就这么个小功能,团队里的人十有八九写错. 上面这句话不是我说的,是梁飞在 ...
- linux登陆欢迎信息及命令提示符修改
登录信息修改 登陆信息显示数据 : /etc/issue and /etc/motd 登陆终端机的时候,会有几行提示的字符串,这些设置在/etc/issue里面可以修改,提示内容在/etc/motd中 ...
- linux在终端中按下键盘立马反应
想在终端中做个小应用,按下上下左右键能立刻作出反应. 测试程序见下: 1 #include <stdio.h> 2 #include <unistd.h> 3 #include ...
- Java并发包源码学习系列:阻塞队列BlockingQueue及实现原理分析
目录 本篇要点 什么是阻塞队列 阻塞队列提供的方法 阻塞队列的七种实现 TransferQueue和BlockingQueue的区别 1.ArrayBlockingQueue 2.LinkedBloc ...
- js input相关事件(转载)
1.onfocus 当input 获取到焦点时触发. 2.onblur 当input失去焦点时触发,注意:这个事件触发的前提是已经获取了焦点再失去焦点的时候才会触发该事件,用于判断标签为空.3.o ...