BZOJ 1857 [Scoi2010]传送带 三分套三分
2015-09-24 07:48
274 查看
题意:
在AB上走的速度是P,在CD上走的速度是Q,在平面走的速度是R,现在询问从A走到D最少花费多长时间。
解析:
模型挺好想?
先查找出来在AB上走到哪比较好,找这个的同时需要再套一个查找找在CD上走到哪好。
但是二分为什么不行?
因为如果我们是二分的话,我们找到一个点后,没有根据使得我们可以向左分或者是向右分,但是这个题来说,假设找到两个点A,B如果走A比走B优的话那么答案一定在AB之间,(据说是物理问题不要问我)
所以我们可以三分出来两个三等分点,然后比较一下再决定向哪边分。
代码:
在AB上走的速度是P,在CD上走的速度是Q,在平面走的速度是R,现在询问从A走到D最少花费多长时间。
解析:
模型挺好想?
先查找出来在AB上走到哪比较好,找这个的同时需要再套一个查找找在CD上走到哪好。
但是二分为什么不行?
因为如果我们是二分的话,我们找到一个点后,没有根据使得我们可以向左分或者是向右分,但是这个题来说,假设找到两个点A,B如果走A比走B优的话那么答案一定在AB之间,(据说是物理问题不要问我)
所以我们可以三分出来两个三等分点,然后比较一下再决定向哪边分。
代码:
#include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define eps 1e-8 using namespace std; double P,Q,R; struct Point { double x,y; Point(){} Point(double _x,double _y):x(_x),y(_y){} friend istream& operator >> (istream &_,Point &a) {scanf("%lf%lf",&a.x,&a.y);return _;} Point operator + (const Point &a) {return Point(x+a.x,y+a.y);} Point operator - (const Point &a) {return Point(x-a.x,y-a.y);} Point operator * (double rate) {return Point(x*rate,y*rate);} double operator * (const Point &a) {return x*a.x+y*a.y;} double operator ^ (const Point &a) {return x*a.y-y*a.x;} friend double get_dis(Point a,Point b) { return sqrt((a-b)*(a-b)); } }A,B,C,D; double calc(Point a) { Point l(C.x,C.y),r(D.x,D.y); while(fabs(r.x-l.x)>eps||fabs(r.y-l.y)>eps) { Point tri1=l+(r-l)*(1.0/3); Point tri2=l+(r-l)*(2.0/3); double cost1=get_dis(A,a)/P+get_dis(a,tri1)/R+get_dis(tri1,D)/Q; double cost2=get_dis(A,a)/P+get_dis(a,tri2)/R+get_dis(tri2,D)/Q; if(cost1>cost2)l=tri1; else r=tri2; } return get_dis(A,a)/P+get_dis(a,l)/R+get_dis(l,D)/Q; } int main() { cin>>A>>B>>C>>D; scanf("%lf%lf%lf",&P,&Q,&R); Point l(A.x,A.y); Point r(B.x,B.y); while(fabs(r.x-l.x)>eps||fabs(r.y-l.y)>eps) { Point tri1=l+(r-l)*(1.0/3); Point tri2=l+(r-l)*(2.0/3); double cost1=calc(tri1); double cost2=calc(tri2); if(cost1>cost2)l=tri1; else r=tri2; } printf("%.2lf\n",calc(l)); }
相关文章推荐
- 工欲善其事,必先利其器。一个强大的开发环境可以大大提高工作效率。好吧,我知道这是废话。。。不过,我想一定有很多跟我一样打算进入Linux平台开发的新手,一开始都为找不到一个像Windows下的VS那样
- 目标检测的图像特征提取之(二)LBP特征
- javaWebSerivice学习篇2-WebService的JDK自带的包开发
- 使用 shared_ptr 的一個問題代碼
- mysql数据库user表host字段的%问题
- BZOJ 1856 [Scoi2010]字符串 组合数
- 栈的基本操作
- 关于 tlb 文件
- Attempting to start Apache service...but apache doesn't work
- shared_ptr Analysis
- 2015年秋季阅读笔记计划
- win7配置自己的IIS服务器亲自做的图文很详细
- BZOJ 1854 [Scoi2010]游戏 二分图最大匹配
- [LeetCode 283] Move Zeroes
- 协同过滤的基本思想
- OpenSSL和OpenSSH
- javaWebSerivice学习篇1-WebService的基础
- 2015年秋季个人阅读计划
- **Palindrome Partitioning II
- hdoj3400Line belt【三分法】