• CF1285B Just Eat It! (最大连续子段和)


    Just Eat It!

    思路:最大连续子段和:判断前缀和是否大于0,如果大于0对后面有贡献,否则置0.

    #include <cstdio>
    #include <iostream>
    #include <cstdio>
    #include <algorithm>
    #include <functional>
    #include <set>
    #include <vector>
    #include <queue>
    #include <cstring>
    #include <stack>
    #include <climits>
     
    using namespace std;
     
    #define ll long long
    #define pb push_back
    #define fi first
    #define se second
    
    void solve(){
    
        int T;
        cin >> T;
        while(T--){
            int n;
            cin >> n;
            vector<int > a(n);
            ll sum = 0;
            for(auto& x : a){
                cin >> x;
                sum += x;
            }
            vector<ll > dp(n + 1);
            ll Max = -1e9 * 1e5 - 1e9;
            for(int i = 1; i < n; ++i){
                if(dp[i - 1] + a[i - 1] > 0) dp[i] = dp[i - 1] + a[i - 1];
                else dp[i] = 0;
                Max = max(Max, dp[i]);
            }
            fill(dp.begin(), dp.end(), 0);
            for(int i = 2; i <= n; ++i){
                if(dp[i - 1] + a[i - 1] > 0) dp[i] = dp[i - 1] + a[i - 1];
                else dp[i] = 0;
                Max = max(Max, dp[i]);
            }
            if(sum > Max) cout << "yes" << endl;
            else cout << "no" << endl;
        }
    }
     
    int main(){
        
        // freopen("C:\Users\admin\Desktop\input.txt", "r", stdin);
        // freopen("C:\Users\admin\Desktop\output.txt", "w", stdout);
        ios::sync_with_stdio(false);
        cin.tie(0);
        cout.tie(0);
        solve();
        
        return 0;
    }
  • 相关阅读:
    4.2 手指绘图
    Linux-linux常用操作
    gdb的使用
    时钟的函数
    动态链接库
    简单算法整理
    win的使用
    c语言格式化打印
    Symfony2 学习笔记之控制器
    Symfony2 学习笔记之系统路由
  • 原文地址:https://www.cnblogs.com/SSummerZzz/p/12843913.html
Copyright © 2020-2023  润新知