题解 | 空间跃迁

空间跃迁

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

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

int main() {
    int n, k, num;
    cin >> n >> k;
    vector<long long>a(n - 1), b;
    for (int i = 0; i < n - 1; i++) {
        cin >> num;
        if (i == 0)
            a[i] = num;
        else
            a[i] = a[i - 1] + num;
    }
    long long max_time = 0;
    if (n- 1 == k )
        cout << 0;
    else {
        if (k > 0) {
            for (int i = k; i < n - 1; i++)
                b.push_back(a[i] - a[i - k]);
            max_time = *max_element(b.begin(), b.end());
        }
        cout << a[n - 2] - max_time;
    }
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务