D - AtCoder Express
Time limit : 2sec / Memory limit : 256MB
Score : 400 points
Problem Statement
In the year 2168, AtCoder Inc., which is much larger than now, is starting a limited express train service called AtCoder Express.
In the plan developed by the president Takahashi, the trains will run as follows:
- A train will run for (t1+t2+t3+…+tN) seconds.
- In the first t1 seconds, a train must run at a speed of at most v1 m/s (meters per second). Similarly, in the subsequent t2 seconds, a train must run at a speed of at mostv2 m/s, and so on.
According to the specifications of the trains, the acceleration of a train must be always within ±1m⁄s2. Additionally, a train must stop at the beginning and the end of the run.
Find the maximum possible distance that a train can cover in the run.
Constraints
- 1≤N≤100
- 1≤ti≤200
- 1≤vi≤100
- All input values are integers.
Input
Input is given from Standard Input in the following format:
N
t1 t2 t3 … tN
v1 v2 v3 … vN
Output
Print the maximum possible that a train can cover in the run.
Output is considered correct if its absolute difference from the judge's output is at most 10−3.
Sample Input 1
1
100
30
Sample Output 1
2100.000000000000000
The maximum distance is achieved when a train runs as follows:
- In the first 30 seconds, it accelerates at a rate of 1m⁄s2, covering 450 meters.
- In the subsequent 40 seconds, it maintains the velocity of 30m⁄s, covering 1200 meters.
- In the last 30 seconds, it decelerates at the acceleration of −1m⁄s2, covering 450 meters.
The total distance covered is 450 + 1200 + 450 = 2100 meters.
Sample Input 2
2
60 50
34 38
Sample Output 2
2632.000000000000000
The maximum distance is achieved when a train runs as follows:
- In the first 34 seconds, it accelerates at a rate of 1m⁄s2, covering 578 meters.
- In the subsequent 26 seconds, it maintains the velocity of 34m⁄s, covering 884 meters.
- In the subsequent 4 seconds, it accelerates at a rate of 1m⁄s2, covering 144 meters.
- In the subsequent 8 seconds, it maintains the velocity of 38m⁄s, covering 304 meters.
- In the last 38 seconds, it decelerates at the acceleration of −1m⁄s2, covering 722 meters.
The total distance covered is 578 + 884 + 144 + 304 + 722 = 2632 meters.
Sample Input 3
3
12 14 2
6 2 7
Sample Output 3
76.000000000000000
The maximum distance is achieved when a train runs as follows:
- In the first 6 seconds, it accelerates at a rate of 1m⁄s2, covering 18 meters.
- In the subsequent 2 seconds, it maintains the velocity of 6m⁄s, covering 12 meters.
- In the subsequent 4 seconds, it decelerates at the acceleration of −1m⁄s2, covering 16 meters.
- In the subsequent 14 seconds, it maintains the velocity of 2m⁄s, covering 28 meters.
- In the last 2 seconds, it decelerates at the acceleration of −1m⁄s2, covering 2 meters.
The total distance covered is 18 + 12 + 16 + 28 + 2 = 76 meters.
Sample Input 4
1
9
10
Sample Output 4
20.250000000000000000
The maximum distance is achieved when a train runs as follows:
- In the first 4.5 seconds, it accelerates at a rate of 1m⁄s2, covering 10.125 meters.
- In the last 4.5 seconds, it decelerates at the acceleration of −1m⁄s2, covering 10.125 meters.
The total distance covered is 10.125 + 10.125 = 20.25 meters.
Sample Input 5
10
64 55 27 35 76 119 7 18 49 100
29 19 31 39 27 48 41 87 55 70
Sample Output 5
20291.000000000000
//题意:读了老半天,就是说一列火车在 n 段路上行驶,给出行驶的时间,和最大速度限制,起点终点速度要为 0 ,问最大可行驶的路程是多少?
//应该是头次做这种题吧, 所以,做的比较慢,先逆序扫一遍,可得到在每段路上行驶,为了到终点为 0 速度,限制速度是多少。
然后正序累加,具体操作是,二分(加速时间+匀速时间)的最大值,然后就可以轻松解决了
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 # include <bits/stdc++.h> 2 using namespace std; 3 # define eps 1e-8 4 # define INF 1e20 5 # define pi acos(-1.0) 6 # define MX 105 7 struct Node 8 { 9 double t,v; 10 double ev; //从终点的最大速度 11 }seg[MX]; 12 13 int n; 14 double spd, ans; 15 16 void gogo(int x) 17 { 18 double l=0, r=seg[x].t; 19 double res = 0; 20 while (l<r-eps) // < 21 { 22 double mid = (l+r)/2; 23 if (mid + spd > seg[x].v-eps) 24 { 25 if ((seg[x].v - seg[x+1].ev) + mid < seg[x].t+eps) 26 { 27 res = mid; 28 l = mid; 29 } 30 else r = mid; 31 } 32 else 33 { 34 if (mid + spd - (seg[x].t-mid) - seg[x+1].ev < -eps) //<= 35 { 36 res = mid; 37 l = mid; 38 } 39 else r = mid; 40 } 41 } 42 43 double jia, yun, jian; 44 if (spd + res < seg[x].v) 45 { 46 jia = res; 47 yun = 0; 48 jian = seg[x].t - jia; 49 } 50 else 51 { 52 jia = seg[x].v - spd; 53 yun = res - jia; 54 jian = seg[x].t - jia - yun; 55 } 56 ans += 1.0/2.0*jia*jia + spd * jia; 57 ans += yun * seg[x].v; 58 59 if (res+spd < seg[x].v) 60 { 61 ans += -1.0/2.0*jian*jian + (spd+jia) * jian; 62 spd = spd + jia - jian; 63 } 64 else 65 { 66 ans += -1.0/2.0*jian*jian + seg[x].v * jian; 67 spd = seg[x].v - jian; 68 } 69 70 } 71 72 int main() 73 { 74 scanf("%d",&n); 75 for (int i=1;i<=n;i++) 76 scanf("%lf",&seg[i].t); 77 for (int i=1;i<=n;i++) 78 scanf("%lf",&seg[i].v); 79 spd=0.0; 80 for (int i=n;i>=1;i--) 81 { 82 if (spd+seg[i].t < seg[i].v+eps) //<= 83 spd += seg[i].t; 84 else 85 spd = seg[i].v; 86 seg[i].ev = spd; 87 } 88 ans = 0; 89 spd = 0; 90 seg[n+1] = (Node){0.0, 0.0, 0.0}; 91 92 for (int i=1;i<=n;i++) 93 { 94 gogo(i); 95 } 96 printf("%.5f ",ans); 97 return 0; 98 }