hdu 6581 Vacation【思维】
原题链接:
http://acm.hdu.edu.cn/showproblem.php?pid=6581
Vacation
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Special Judge
Problem Description
Tom and Jerry are going on a vacation. They are now driving on a one-way road and several cars are in front of them. To be more specific, there are n cars in front of them. The ith car has a length of li, the head of it is si from the stop-line, and its maximum velocity is vi. The car Tom and Jerry are driving is l0 in length, and s0 from the stop-line, with a maximum velocity of v0.
The traffic light has a very long cycle. You can assume that it is always green light. However, since the road is too narrow, no car can get ahead of other cars. Even if your speed can be greater than the car in front of you, you still can only drive at the same speed as the anterior car. But when not affected by the car ahead, the driver will drive at the maximum speed. You can assume that every driver here is very good at driving, so that the distance of adjacent cars can be kept to be 0.
Though Tom and Jerry know that they can pass the stop-line during green light, they still want to know the minimum time they need to pass the stop-line. We say a car passes the stop-line once the head of the car passes it.
Please notice that even after a car passes the stop-line, it still runs on the road, and cannot be overtaken.
Input
This problem contains multiple test cases.
For each test case, the first line contains an integer n (1≤n≤105,∑n≤2×106), the number of cars.
The next three lines each contains n+1 integers, li,si,vi (1≤si,vi,li≤109). It's guaranteed that si≥si+1+li+1,∀i∈[0,n−1]
Output
For each test case, output one line containing the answer. Your answer will be accepted if its absolute or relative error does not exceed 10−6.
Formally, let your answer be a, and the jury's answer is b. Your answer is considered correct if |a−b|max(1,|b|)≤10−6.
The answer is guaranteed to exist.
Sample Input
1
2 2
7 1
2 1
2
1 2 2
10 7 1
6 2 1
Sample Output
3.5000000000
5.0000000000
题意:n辆车按前后顺序在一条单行道上行驶,若后一辆车碰到了前一辆车的尾巴,也必须按前一辆车的速度行驶,不能超越,按离终点距离从大到小的顺序给你n+1辆车的车身长,车头离终点距离,和各自的速度(你是离终点最远的那一辆车),问你冲过终点的最短时间。
思路:这道题我们不应该把重点放在每一辆车何时冲过终点,不然这题会变得非常复杂,而是应该考虑冲过终点后它何时能留出足够的空间让后面的车放下,这才是我们真正应该求的临界状态,比如第一辆车长度是3,后面一辆长度是2,则第一辆车至少要车头冲过终点线并且再走5的长度才能让后面一辆车放下(它自己的车身3+后面那辆车的长度2),说的明白点,就是至少要再跑除了我们自己那辆车之外的所有车的长度(因为我们自己的车只要冲过终点就行了,所以前面的车不需要给我们的车留空间),这样就能让后面的车全部都跑出来,这样我们的车才能冲过终点。要是冲过那个临界位置我们就不用管了呀,所以我们只要考虑临界位置就好了。那么问题又来了,要是后面的车被前面的车堵住了怎么办?我们可以先思考一下,什么情况下后面一辆车会被前面一辆车给堵住?当然是后面一辆车到达它自己应该到的那个位置时,前面一辆车却没有到达前面一辆车它自己应该到达的位置(也就是各自的临界位置),这样他们肯定在行驶过程中相碰了。所以真正用的时间就是最慢到达自己临界位置的那辆车所用的时间(因为后面的车全部被这辆“拖拉机”堵住了),如此类推下去,我们需要的时间就是每一辆车达到它应该到达的位置的时间的最大值,此题结束。
代码:
#include "stdio.h"
#include "algorithm"
using namespace std;
const int N=1e5+;
int n,l[N],s[N],v[N],sum[N];
double t,t1; int main() {
while(~scanf("%d", &n))
{
t=-; ///初始化一下答案
for (int i = ; i <= n + ; i++)
scanf("%d", &l[i]);
for (int i = ; i <= n + ; i++)
scanf("%d", &s[i]);
for (int i = ; i <= n + ; i++)
scanf("%d", &v[i]);
for(int i=;i<=n+;i++)
sum[i]=sum[i-]+l[i]; ///先把车身长度也就是每辆车需要预留的位置给预处理出来
for(int i=n+;i>=;i--)
{
t1=(s[i]+sum[i])*1.0/v[i]; ///这辆车到达它应该到达的位置的位置所用的时间
t=max(t,t1); ///找出那辆最慢的“拖拉机”,当i=1时,t代表我们没有被挡直接冲过终点所用的时间
}
printf("%.10f\n",t);
}
return ;
}
谢谢访问,如果觉得好的话可以点个赞哦,有不懂的也可以在下面提问,互相学习,共同进步,谢谢大家的支持!
hdu 6581 Vacation【思维】的更多相关文章
- HDU 6581 Vacation
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total Submission( ...
- 【HDU - 6581】Vacation(思维)
Vacation 题意 有n+1辆车,属性有长度l,距离终点的距离s,速度v问你最末尾的车到达终点的时间 Sample Input 1 2 2 7 1 2 1 2 1 2 2 10 7 1 6 2 1 ...
- hdu 6851 Vacation(思维+贪心)
传送门 •题意 有编号0到n,n+1辆车排队过红绿灯,从0到n离交通灯线越来越近 每辆车都有一个最大速度v,车身长度l,和离交通灯线的距离s, 一辆车头到达线则说明这辆车已到达线 如果一辆车前面没有紧 ...
- HDU 5776 sum (思维题)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5776 题目让你求是否有区间的和是m的倍数. 预处理前缀和,一旦有两个数模m的值相同,说明中间一部分连续 ...
- hdu 4803 贪心/思维题
http://acm.hdu.edu.cn/showproblem.php?pid=4803 话说C++还卡精度么? G++ AC C++ WA 我自己的贪心策略错了 -- 就是尽量下键,然后上 ...
- Binary Tree HDU - 5573 (思维)
题目链接: B - Binary Tree HDU - 5573 题目大意: 给定一颗二叉树,根结点权值为1,左孩子权值是父节点的两倍,右孩子是两倍+1: 给定 n 和 k,让你找一条从根结点走到第 ...
- HDU 5178 pairs —— 思维 + 二分
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5178 pairs Time Limit: 2000/1000 MS (Java/Others) ...
- ACM-ICPC 2016 大连赛区现场赛 K. Guess the number && HDU 5981(思维+DP)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5981 题意:A在[L, R]之间随机选取一个数X,之后B来猜这个数,如果猜的数比X小,那么A就告诉B猜 ...
- ACM-ICPC 2017 沈阳赛区现场赛 M. Wandering Robots && HDU 6229(思维+期望)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6229 参考题解:https://blog.csdn.net/lifelikes/article/det ...
随机推荐
- Flutter Web环境搭建
接上篇Flutter Windows下AndroidStudio环境搭建 1.https://github.com/flutter/flutter_web 下载放到本地路径下 2.系统Path增加(根 ...
- 99乘法表(for循环嵌套)
计算乘法表 两个数相乘,外层循环代表被乘数,控制行数:内层代表乘数,控制列数. 循环嵌套,变量名不可以重复. 使用 break 语句让输出的乘法表更简洁. 使用 String 变量,做 String ...
- java.io.IOException: Error: JSP Buffer overflow
错误 jsp页面报错如下: Stacktrace: org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java ...
- sql语句中 and 与or 的优先级
- 你从未见过的Case Study写作指南
Case Study,意为案例分析,Case Study与其它的留学论文作业最大的的差别就在于Case Study在论文开始就需要明确给出论,然后再阐述这个结论的论证依据和理由.留学生们需要知道的是C ...
- unity基础开发----Unity获取PC,Ios系统的mac地址等信息
在软件开发中可以会用到mac地址作为,设备的唯一标示,我们也可以通过unity获取,经测试pc,ios都可以但是安卓没有获取到. 代码如下: using UnityEngine; using Syst ...
- 说说我当初是如何学Linux的
今天我就说说我当初是如何从一名普通桌面维护工程师,通过学习和努力转成Linux运维工程师的,以及作为Linux运维工程师需要一些什么技能和知识,希望可以帮到一些对Linux有兴趣或者想往Linux这个 ...
- Linux 安装gcc g++
Linux版本: cat /proc/version Linux version 3.10.0-693.11.6.el7.x86_64 (mockbuild@x86-041.build.eng.bos ...
- DP(动态规划求含有冻结期的买卖股票)-05-动态规划-买卖股票
题目描述 Alice这次决定去股市里当一波韭菜. 她希望你设计一个算法,在满足以下3个约束条件下,计算出最大利润. 1. 你可以多次买卖一支股票,但是对于每支股票,你不能同时参与多笔交易(你必须在再 ...
- 八十一、SAP中的ALV的简介(ABAP List Viewer)
一.ALV是SAP中的一个表格,全称为:ABAP List Viewer或者SAP List Viewer,就是可视化表格. ALV是SAP系统中心的列表标准,可以在ABAP程序中进行报表输出.除去列 ...