E - E ZOJ - 2592

本文介绍了E-EZOJ-2592问题的解决思路及算法实现,该问题涉及一系列正负数值的累积计算,通过调整起始点使全年积极情绪占主导地位。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

E - EZOJ - 2592

 

It is well known, that the year on planet Eisiem has n days. Of course, some days are very good for people, while some others are just horrible. Long observations have shown for each day of the year whether this day is good for most people, or bad.

The new president of the Planet Federation wants all people to be happy. He knows that good emotions have a tendency to accumulate, just like bad ones do. The New Year however is a special event and all emotions accumulated by this moment just disappear. Therefore the president wants to change the calendar on Eisiem and choose the new first day of the year, so that the positive emotions would prevail the whole year.

More precisely, for all i from 1 to n let ai be 1 if i-th day is good for most people and -1 if it is bad. Let sjk be the sum of aifor all days from the j-th day of the year to the k-th, that is:

President wants to find such j to order the j-th day to be the first day of the year, that sjk is positive for all k from 1 to n. Since he wants several variants to choose from, he asks you to find all such j. Since he doesn't want to get too much information at once, first of all he wants to know the number of such j. That is exactly your task.


Input

The input contains multiple test cases. The first line of the input is a single integer T (1 <= T <= 30) which is the number of test cases. T test cases follow, each preceded by a single blank line.

The first line of each test case contains n - the number of days (1 <= n <= 200 000). Next line contains n integer numbers - ai.


<p< dd="">Output

For each case, output the number of different indices j, such that sjk is positive for all k, in a single line. 

<b< dd="">

Sample Input

2

5
1 -1 1 -1 1

7
1 1 1 1 1 1 1

<p< dd="">Sample Output

1
7

题意:

是让你求 Aj在K[0-n]的范围内 Sjk都>0 的个数;  

其实把这些数顺时针放在圆周上,要每个区间的和都是>0的,即不管从哪出开始扫描结果都是>0;


结果:

就是求这些数相加的和;

在1和-1数目相等的时候刚好是0个j满足,然后增加一个1时可以用反证法证明恰好存在一个j.

可以用数学归纳法证明:设和为n时,j的个数为f(n),f(n)=n;


代码:

#include<map>
#include<stdio.h>
#include<string>
#include<string.h>
#include <iostream>
#include<algorithm>
using namespace std;
int main()
{
    int ca;
    scanf("%d",&ca);
    while(ca--)
    {
        int n,x,s=0;
        scanf("%d",&n);
        for(int i=0;i<n;i++)
        {
            scanf("%d",&x);
            s+=x;
        }
        s=max(s,0);    //如果<0,则为0;
        printf("%d\n",s);
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值