码迷,mamicode.com
首页 > 其他好文 > 详细

HackerRank - "Chief Hopper"

时间:2015-05-27 07:26:06      阅读:198      评论:0      收藏:0      [点我收藏+]

标签:

Apparently it is by a backwards derivation solution. Say energy at h[i] is e, the next energy at h[i+1] is 2*e - h[i+1] => e‘, so backwards, e = ceil((e‘ + h[i + 1])/2). We assume energy is 0 at h[n-1]:

#include <string>
#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
#include <queue>
#include <unordered_map>
#include <functional>
using namespace std;

int main()
{
    int n; cin >> n;
    vector<int> hs(n);
    for (int i = 0; i < n; i++)
        cin >> hs[i];

    long long ret = ceil(hs[n - 1] / 2.0);
    for (int i = n - 2; i >= 0; i--)
        ret = ceil((hs[i] + ret) / 2.0);

    cout << ret << endl;
    return 0;
}

HackerRank - "Chief Hopper"

标签:

原文地址:http://www.cnblogs.com/tonix/p/4532359.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!