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 ...
随机推荐
- python学习笔记 | PyCharm创建文件时自动添加头文件
File Settings Editor File and Code Templates Python Script 然后在右边的框中写入信息就可以啦: # -*- coding: utf-8 -*- ...
- HashMap为什么效率高?来看看这个小demo
一.前情回顾:在程序中有时候需要存放对象,容器应运而生.容器分为集合和Map.集合在这里不说,说说Map.Map在英语中是地图的意思,这个名字真是起的好,可以让人顾名思义.Map,就是存放键值对的结构 ...
- QPinter 常用绘制图像的方法
阅读本文大概需要 3 分钟 我们在开发软件的过程中,绘制图像功能必不可少,使用 Qt 绘制图像时非常简单,只需要传递几个参数就可以实现功能,在 Qt 中绘制图像的 api有好几个 void drawI ...
- 【Linux】ABRT has detected 1 problem(s). For more info run: abrt-cli list --since 1548988705
------------------------------------------------------------------------------------------------- | ...
- 【Linux】用find删除大于30天的文件
1.删除文件命令: find 对应目录 -mtime +天数 -name "文件名" -exec rm -rf {} \; 实例命令:find /opt/soft/log/ -mt ...
- leetcode 31. Next Permutation (下一个排列,模拟,二分查找)
题目链接 31. Next Permutation 题意 给定一段排列,输出其升序相邻的下一段排列.比如[1,3,2]的下一段排列为[2,1,3]. 注意排列呈环形,即[3,2,1]的下一段排列为[1 ...
- LeetCode653. 两数之和 IV - 输入 BST
题目 直接暴力 1 class Solution { 2 public: 3 vector<int>ans; 4 bool findTarget(TreeNode* root, int k ...
- SDUST数据结构 - chap8 查找
选择题: 函数题: 6-1 二分查找: 裁判测试程序样例: #include <stdio.h> #include <stdlib.h> #define MAXSIZE 10 ...
- ctfhub技能树—RCE—命令注入
打开靶机 查看页面信息 输入127.0.0.1进行测试 构造payload 127.0.0.1&ls 查看文件内容信息 127.0.0.1 & cat 179852221619745. ...
- 24V转3.3V稳压芯片,高效率同步降压DC-DC变换器3A输出电流
PW2312是一个高频,同步,整流,降压,开关模式转换器与内部功率MOSFET.它提供了一个非常紧凑的解决方案,以实现1.5A的峰值输出电流在广泛的输入电源范围内,具有良好的负载和线路调节. PW23 ...