FZU - 1914

题意略。

思路:

我们应该着重关注负数对当前数列的影响,由于前缀和的性质,我们都是从当前数字向前加,这其实也是在枚举以哪个下标作为开头。

详见代码:

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<algorithm>
using namespace std;
typedef long long LL;
const int maxn = 1e6 + 5;

int T,n,cas = 1;
LL store[maxn];
bool visit[maxn];

int main(){
    scanf("%d",&T);
    while(T--){
        scanf("%d",&n);
        for(int i = 0;i < n;++i){
            scanf("%I64d",&store[i]);
            store[i + n] = store[i];
        }
        memset(visit,false,sizeof(visit));
        LL sum = 0;
        for(int i = 2 * n - 1;i >= 0;--i){
            sum += store[i];
            if(sum <= 0){
                visit[i % n] = true;
            }
            else sum = 0;
        }
        int ans = n;
        for(int i = 0;i < n;++i){
            if(visit[i]) --ans;
        }
        printf("Case %d: %d\n",cas++,ans);
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/tiberius/p/9343328.html
今日推荐