牛客——动态规划专项

NC1 连续子数组最大和(ACM版本)

连续子数组最大和(ACM版本)_牛客题霸_牛客网【牛客题霸】收集各企业高频校招笔面试题目,配有官方题解,在线进行百度阿里腾讯网易等互联网名企笔试面试模拟考试练习,和牛人一起讨论经典试题,全面提升你的技术能力icon-default.png?t=LA92https://www.nowcoder.com/practice/1718131e719746e9a56fb29c40cc8f95?tpId=230&tqId=2225856&ru=/activity/oj&qru=/ta/dynamic-programming/question-ranking

解析:注意一下,数据范围

#include <stdio.h>
#include <stdlib.h>

int main(){
    int n, a[100005];
    long long int sum[100005] = {0};
    scanf("%d", &n);
    for(int i = 0; i < n; i++){
        scanf("%d", &a[i]);
    }
    sum[0] = a[0];
    for(int i = 1; i < n; i++){
        if(a[i] <= sum[i-1] + a[i]){
            sum[i] = sum[i-1] + a[i];
        }else{
            sum[i] = a[i];
        }
    }
    long long int max = -9999;
    for(int i = 0; i < n; i++){
        if(max < sum[i]){
            max = sum[i];
        }
    }
    printf("%lld", max);
    return 0;
}

NC2 不相邻取数

猜你喜欢

转载自blog.csdn.net/qq_41664159/article/details/121877281