Covered Path
Description
The on-board computer on Polycarp's car measured that the car speed at the beginning of some section of the path equals v1 meters per second, and in the end it is v2meters per second. We know that this section of the route took exactly t seconds to pass.
Assuming that at each of the seconds the speed is constant, and between seconds the speed can change at most by d meters per second in absolute value (i.e., the difference in the speed of any two adjacent seconds does not exceed d in absolute value), find the maximum possible length of the path section in meters.
Input
The first line contains two integers v1 and v2 (1 ≤ v1, v2 ≤ 100) — the speeds in meters per second at the beginning of the segment and at the end of the segment, respectively.
The second line contains two integers t (2 ≤ t ≤ 100) — the time when the car moves along the segment in seconds, d (0 ≤ d ≤ 10) — the maximum value of the speed change between adjacent seconds.
It is guaranteed that there is a way to complete the segment so that:
- the speed in the first second equals v1,
- the speed in the last second equals v2,
- the absolute value of difference of speeds between any two adjacent seconds doesn't exceed d.
Output
Print the maximum possible length of the path segment in meters.
Examples
Input
5 6
4 2
Output
26
Input
10 10
10 0
Output
100
Note
In the first sample the sequence of speeds of Polycarpus' car can look as follows: 5, 7, 8, 6. Thus, the total path is 5 + 7 + 8 + 6 = 26 meters.
In the second sample, as d = 0, the car covers the whole segment at constant speed v = 10. In t = 10 seconds it covers the distance of 100 meters.
描述:
一个小车起始速度为v1,末速度为v2,给出时间t,和最大加速度(-d--d)
求小车在 t 时间内的最大路程。
正确解法:
时间从 t==2 开始枚举,从d— -d开始枚举加速度。
当满足 现在的速度v 加上加速度 再减去剩余时间*d <=v2 时,此时的加速度就是目前所能加的最大加速度。
万一小车加速,要保证它在剩余时间内可以减速到 v2
(第一次见到枚举加速度,要好好记下来)
1 #include<iostream> 2 #include<cstdio> 3 #include<cmath> 4 #include<algorithm> 5 #include<string> 6 #include<cstring> 7 using namespace std; 8 int b[1000010] = {0}; 9 int main() 10 { 11 int v1, v2,t,d,v; 12 cin >> v1 >> v2 >> t >> d; 13 long long ans = v1; 14 v = v1; t=t-1; 15 while (t) { 16 for (int i = d; i >= -d; i--) 17 if (v + i - (t - 1)*d <= v2) 18 { 19 ans += v + i; 20 t--; 21 v = v + i; 22 break; 23 } 24 } 25 cout << ans << endl; 26 return 0; 27 }