zoukankan      html  css  js  c++  java
  • B. Just Eat It! CodeForces1285B

    题意:给你一个序列,他的序列和是否大于他的任何他的子序列(连续的子序列)的和。

    题目链接:https://vjudge.net/problem/CodeForces-1285B

    思路:求他的最大连续子序列和。那如何求最大子序列和呢?用动态规划求即可。但是要注意的是,子系列不能和原序列一样。

    用dp[i]表示以a[i]结尾的最大连续子序列,则容易列出动态转移方程dp[i] = max{dp[i - 1] + a[i], a[i]};

    分别求[0, n-1)和[2,n-1]的最大连续子序列和就可以了

    #include<iostream>
    #include <cstdlib>
    #include <algorithm>
    using namespace std;
    typedef long long ll;
    const int N = 1e5 + 5;
    ll a[N] = {0};
    int main()
    {
        int t;
        scai(t);
        while (t--)
        {
            int n;
            scai(n);
            ll sum = 0;
            for (int i = 0; i < n; i++)
            {
                scanf("%lld", &a[i]);
                sum += a[i];
            }
    
            ll ans = 0;
            string s = "YES";
            for (int i = 0; i < n; ++i)
            {
                ans += a[i];
                if (ans >= sum && i != n-1)
                    {s = "NO";break;}
                if (ans  < 0) ans = 0;
            }
            ans = 0;
            for (int i = n - 1; i >= 0; --i)
            {
                ans += a[i];
                if (ans >= sum && i != 0)
                    {s = "NO";break;}
                if (ans < 0) ans = 0;
            }
            cout << s <<endl;
        }
    }
  • 相关阅读:
    一道题DP
    BZOJ 3155: Preprefix sum
    BZOJ:3209: 花神的数论题
    TJU 4087. box
    BZOJ1192: [HNOI2006]鬼谷子的钱袋
    概率DP
    Codeforces Round #253 (Div. 2) D题
    二维树状数组
    Codeforces Round #250 (Div. 2)
    莫比乌斯函数
  • 原文地址:https://www.cnblogs.com/hulian425/p/12208165.html
Copyright © 2011-2022 走看看