HDU 5783 Divide the Sequence——贪心

来源:互联网 发布:chrome js 拷贝 编辑:程序博客网 时间:2024/05/25 23:25

这道题关键要理解题意:每个子串的【所有前缀和】都大于等于0,比如1 1 -2,满足1>=0, 1 + 1>=0, 1 + 1 + -2 >= 0

明白这一点后,就能很自然的想出倒着扫的策略

最后注意用long long

#include <cstdio>using namespace std;typedef long long ll;const int MAXN = 1e6 + 10;int T, N;ll a[MAXN];int main() {    while (~scanf("%d", &N)) {        for (int i = 1; i <= N; i++) scanf("%I64d", &a[i]);        int ans = 0;        for (int i = N; i >= 1; i--) {            if (a[i] >= 0) ans++;            else {                a[i - 1] += a[i];            }        }        printf("%d\n", ans);    }    return 0;}


原创粉丝点击