Codeforces534B: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.

Sample test(s)

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?=?26meters.

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 ,给出总共行驶的时间,而且每秒的速度增加与减少最多为d,要求出最大的行驶距离

思路:我们假设最高速度为第i秒时,那么从1~i是一个以v1为起始项的的递增序列,而从t~i则是以v2为起始项的递增序列,那么枚举i的位置求出来的值必然是最大的

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <math.h>
#include <algorithm>
using namespace std;
#define ls 2*i
#define rs 2*i+1
#define up(i,x,y) for(i=x;i<=y;i++)
#define down(i,x,y) for(i=x;i>=y;i--)
#define mem(a,x) memset(a,x,sizeof(a))
#define w(a) while(a)
#define LL long long
const double pi = acos(-1.0);
#define Len 63
#define mod 19999997
const int INF = 0x3f3f3f3f;

int v1,v2,t,d,sum;

int main()
{
    int i,j,k;
    w(~scanf("%d%d%d%d",&v1,&v2,&t,&d))
    {
        sum = 0;
        up(i,0,t-1)
        sum+=min(v1+i*d,v2+(t-i-1)*d);
        printf("%d\n",sum);
    }
    return 0;
}

 

时间: 2024-10-10 06:56:24

Codeforces534B:Covered Path的相关文章

Codeforces 534B Covered Path(贪心)

题意  你在路上走 每秒钟的开始都可以改变自己的速度(改变速度都是瞬间完成的)  知道你开始的速度v1 结束时的速度v2  整个过程所用时间t  以及每秒最多改变的速度d  求这段时间内你最多走了多远 最优的肯定是先把速度从v1升到最大  然后从最大减到v2  使得用的时间不会超多t   因为肯定是足够从v1减为或升到v2的   那么我们只用从两端往中间靠  哪边的速度小  哪边就加上d  知道两边相邻  这样就能保证每次改变的速度都最大  而且最后两端的速度差也不会大于d  也就是最优答案了

Codeforces 534B - Covered Path

534B - Covered Path 思路:贪心,每一秒取尽可能大速度. 画张图吧,不解释了: 代码: #include<bits/stdc++.h> using namespace std; #define ll long long int dp[105],dp1[105]; int main() { ios::sync_with_stdio(false); cin.tie(0); int v1,v2,t,d; cin>>v1>>v2>>t>>

[VJ][暴力枚举]Covered Path

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 to

Codeforces Round #298 (Div. 2) B. Covered Path

题目大意: 一辆车,每秒内的速度恒定...第I秒到第I+1秒的速度变化不超过D.初始速度为V1,末速度为V2,经过时间t,问最远能走多远. 分析 开始的时候想麻烦了.讨论了各种情况.后来发现每个时刻的最大值都满足一定的约束关系.设此时为时刻i,上一次的速度为p,那么本次的速度应为max(p+d,v2+(t-i)*d),因为要保证最终一定能够返回到v2.这样以来便可以得到每个时刻的最大值,然后累加求和即可. 1 #include<cstdio> 2 #include<iostream>

#298 (div.2) B. Covered Path

1.题目描述:点击打开链接 2.解题思路:本题利用贪心法解决.一开始想着利用dfs解决,不过最终意料之中地TLE了,因为每次可以选择的速度增加量有很多,一共2*d个,这样的话,时间复杂度是O(T*D^T)达到了指数级别.所以应当改变思路,想办法求出每个时刻的最大值.通过尝试可以发现,每个时刻的最大值都满足一定的约束关系.设此时为时刻i,上一次的速度为p,那么本次的速度应为max(p+d,v2+(t-i)*d),因为要保证最终一定能够返回到v2.这样以来便可以得到每个时刻的最大值,然后累加求和即可

Codeforces Round #298 (Div. 2)

A - Exam 构造 1 #include <cstdio> 2 3 int ans[5000 + 5]; 4 5 int main() { 6 int n, cnt = 1; 7 scanf("%d", &n); 8 for (int i = 1; i <= n; i++) { 9 if (i&1) ans[i] = cnt; 10 else { 11 ans[i] = n+1-cnt; 12 cnt++; 13 } 14 } 15 ans[0]

Codeforces Round #298 (Div. 2) A、B、C题

题目链接:Codeforces Round #298 (Div. 2) A. Exam An exam for n students will take place in a long and narrow room, so the students will sit in a line in some order. The teacher suspects that students with adjacent numbers (i and i + 1) always studied side

Linux下修改环境变量PATH

1.什么是环境变量(PATH) 在Linux中,在执行命令时,系统会按照PATH的设置,去每个PATH定义的路径下搜索执行文件,先搜索到的文件先执行. 我们知道查阅文件属性的指令ls 完整文件名为:/bin/ls(这是绝对路径), 那你会不会觉得很奇怪:"为什么我可以在任何地方执行/bin/ls这个指令呢? " 为什么我在任何目录下输入 ls 就一定可以显示出一些讯息而不会说找不到该 /bin/ls 指令呢? 这是因为环境变量 PATH 的帮助所致呀! 当我们在执行一个指令癿时候,举例

Description Resource Path Location Type The superclass &quot;javax.servlet.http.HttpServlet&quot; was not foun

一段时间没亲自建新项目玩乐,今天建立了一Maven project的时候发现了以下异常,Description Resource Path Location Type The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path index.jsp /easyBuy/src/main/webapp line 1 JSP Problem 经过查找原因,原来是因为忘记设置server