题解 | #小红的小踏前斩#

小红的小踏前斩

https://www.nowcoder.com/practice/aa428da6310a4e889bad5bae59f89025

#include <algorithm>
#include <climits>
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;

int main() {
    int n;
    cin >> n;
    int temp = n;
    vector<int> ret(n);
    for (auto& num : ret) {
        cin >> num;
    }
    int minNum = ret[0] > ret[1] / 2 ? ret[0] : (ret[1] + 1) / 2;
    int t1, t2;
    for (int i = 0; i < n - 2; i++) {
        if (ret[i + 1] > ret[i + 2] / 2) {
            t2 = (ret[i + 2] + 1) / 2;
            t1 = (ret[i + 1] - t2 + 1) / 2;
            minNum = min(t1 + t2, minNum);
        } else {
            t2 = (ret[i + 2] + 1) / 2;
            minNum = min(t2, minNum);
        }
    }
    int first = ret[0];
    sort(ret.begin(), ret.end());
    if (ret[0] != first && ret[1] != first) {
        minNum = min((ret[0] + 1) / 2 + (ret[1] + 1) / 2, minNum);
    } else if (ret[0] == first) {
        minNum = min(min(first + (ret[1] + 1) / 2, (ret[1] + 1) / 2 + (ret[2] + 1) / 2),
                     minNum);
    } else if (ret[1] == first) {
        minNum = min(min(first + (ret[0] + 1) / 2, (ret[0] + 1) / 2 + (ret[2] + 1) / 2),
                     minNum);
    }
    cout << minNum << endl;

}


全部评论

相关推荐

怎么起名字:学历不足,
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务