Codeforces 534B Covered Path(貪心)

來源:互聯網
上載者:User

標籤:acm   貪心   

題意  你在路上走 每秒鐘的開始都可以改變自己的速度(改變速度都是瞬間完成的)  知道你開始的速度v1 結束時的速度v2  整個過程所用時間t  以及每秒最多改變的速度d  求這段時間內你最多走了多遠

最優的肯定是先把速度從v1升到最大  然後從最大減到v2  使得用的時間不會超多t   因為肯定是足夠從v1減為或升到v2的   那麼我們只用從兩端往中間靠  哪邊的速度小  哪邊就加上d  知道兩邊相鄰  這樣就能保證每次改變的速度都最大  而且最後兩端的速度差也不會大於d  也就是最優答案了

#include <bits/stdc++.h>using namespace std;const int N = 105;int v[N];int main(){    int v1, v2, t, d, k;    while(~scanf("%d%d%d%d", &v1, &v2, &t, &d))    {        v[0] = v1, v[t - 1] = v2;        int le = 0, ri = t - 1, ans = 0;        while(le < ri - 1)        {            if(v[le] < v[ri]) v[le + 1] = v[le++] + d;            else v[ri - 1] = v[ri--] + d;        }        for(int i = 0; i < t; ++i) ans += v[i];        printf("%d\n", ans);    }    return 0;}//Last modified :   2015-04-13 00:27

B. 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 64 2
output
26
input
10 1010 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.




Codeforces 534B Covered Path(貪心)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.