Covered Path
题目描述
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 v2 meters 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,问你这辆车在t时间内最大行驶路程是多少。(注意,速度的改变是整秒)
思路,极端的来说当你以最大加速度进行加速和减速时,t时间行程是最大的,但你要保证最后能回到v2速度,所以我们考虑用dp来做。每一时刻的最大速度为前一秒的速度加上最大加速度,与从末尾v2以最大加速度增加到此时刻的速度,取两者之间的较小值。最后将每一秒的速度相加即可。
下面是代码
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<vector>
#include<cmath>
#include<string>
#include<set>
#define eps 1e-7
#define LD long double
#define LL long long
#define maxn 200050
using namespace std;
int main()
{
int v1,v2,v,t,d;
int sum=0;
int f[105];
cin>>v1>>v2>>t>>d;
f[1]=v1;//初始时刻速度为v1
f[t]=v2;//结束时刻速度为v2
for(int i=2;i<t;i++)
{
v=f[i-1]+d;
if(v>(t-i)*d+v2)//如果前者大于后者,说明此时最大速度只能是后者
v=(t-i)*d+v2;
f[i]=v;
}
for(int i=1;i<=t;i++)//累加输出结果
sum+=f[i];
cout<<sum;
return 0;
}