There are n integers a 1,a 2,…,a n-1,a n in the sequence A, the sum of these n integers is larger than zero. There are n integers b 1,b 2,…,b n-1,b n in the sequence B, B is the generating sequence of A and bi = a 1+a 2,+…+a i (1≤i≤n). If the elements of B are all positive, A is called as a positive sequence.

We left shift the sequence A 0,1,2,…,n-1 times, and get n sequences, that is showed as follows:

A(0): a1,a2,…,an-1,an

A(1): a2,a3,…,an,a1

A(n-2): an-1,an,…,an-3,an-2

A(n-1): an,a1,…,an-2,an-1

Your task is to find out the number of positive sequences in the set { A(0), A(1), …, A(n-2), A(n-1) }.

Input

The first line of the input contains an integer T (T <= 20), indicating the number of cases. Each case begins with a line containing one integer n (1 <= n <= 500,000), the number of elements in the sequence. The next line contains n integers ai(-2,000,000,000≤ai≤2,000,000,000,1≤i≤n), the value of elements in the sequence.

Output

For each test case, print a line containing the test case number (beginning with 1) and the number of positive sequences.

Sample Input

2
3
1 1 -1
8
1 1 1 -1 1 1 1 -1

Sample Output

Case 1: 1
Case 2: 4

思路:可以建立一个长度为2*n的数组,每次查询它的前ai项之和是不是负数,如果是负数,

用vis标记,可以保证后面扫的时候,扫到这个地方就可以停下了

代码:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<stack>
#include<set>
#include<map>
#include<vector>
#include<cmath> const int maxn=5e5+;
typedef long long ll;
using namespace std;
ll a[*maxn];
int vis[maxn];
int main()
{
int t,cnt=,i,n,j;
scanf("%d",&t);
while(t--)
{
memset(a,,sizeof(a));
memset(vis,,sizeof(vis));
scanf("%d",&n);
for(i=;i<n;i++)
{
scanf("%I64d",&a[i]);
a[i+n]=a[i];
}
for(i=*n-;i>=n;i--)
{
if(i<n) break;
if(a[i]<=)
{
ll sum=;
for(j=i;j>i-n;j--)
{
sum+=a[j];
if(sum<=)
{
if(j<n) vis[j]=;
else vis[j-n]=;
}
else break;
}
i=j;
}
if(i<n) break;
}
int ans=;
for(i=;i<n;i++)
{
if(vis[i]==) ans++;
} printf("Case %d: %d\n",cnt++,n-ans);
} return ;
}

Funny Positive Sequence (思维+前缀)的更多相关文章

  1. FZU 1914 Funny Positive Sequence

    题目链接:Funny Positive Sequence 题意:给出一个数列,如果它的前i(1<=i<=n)项和都是正的,那么这个数列是正的,问这个数列的这n种变换里, A(0): a1, ...

  2. [JZOJ5280]膜法师题解--思维+前缀和

    [JZOJ5280]膜法师题解--思维+前缀和 题目链接 暴 力 过 于

  3. HDU - 6025 Coprime Sequence(前缀gcd+后缀gcd)

    题意:去除数列中的一个数字,使去除后数列中所有数字的gcd尽可能大. 分析:这个题所谓的Coprime Sequence,就是个例子而已嘛,题目中没有任何语句说明给定的数列所有数字gcd一定为1→_→ ...

  4. AtCoder Beginner Contest 124 D - Handstand(思维+前缀和)

    D - Handstand Time Limit: 2 sec / Memory Limit: 1024 MB Score : 400400 points Problem Statement NN p ...

  5. C. Multi-Subject Competition 思维+前缀和+填表加减复杂度(复杂度计算错误)

    题意: 给出n个学生 m类题目 每个人会做s[i]类的题 并且做这个题的能力为r[i]  组成一个竞赛队 要求可以选择一些题目  在竞赛队中 擅长每一个题目的 人数要均等  求max(sigma(r[ ...

  6. Codeforces 776C - Molly's Chemicals(思维+前缀和)

    题目大意:给出n个数(a1.....an),和一个数k,问有多少个区间的和等于k的幂 (1 ≤ n ≤ 10^5, 1 ≤ |k| ≤ 10, - 10^9 ≤ ai ≤ 10^9) 解题思路:首先, ...

  7. Codeforces_776_C_(思维)(前缀和)

    C. Molly's Chemicals time limit per test 2.5 seconds memory limit per test 512 megabytes input stand ...

  8. 2019牛客多校训练第三场B.Crazy Binary String(思维+前缀和)

    题目传送门 大致题意: 输入整数n(1<=n<=100000),再输入由n个0或1组成的字符串,求该字符串中满足1和0个数相等的最长子串.子序列. sample input: 801001 ...

  9. atcode E - guruguru(思维+前缀)

    题目链接:http://arc077.contest.atcoder.jp/tasks/arc077_c 题解:一道思维题.不容易想到类似区间求和具体看一下代码. #include <iostr ...

随机推荐

  1. Docker技术入门与实战

      Docker技术入门与实战 下载地址https://pan.baidu.com/s/1bAoRQQlvBa-PXy5lgIlxUg 扫码下面二维码关注公众号回复100011 获取分享码 本书目录结 ...

  2. OO第一单元(前四周)作业总结

    OO第一单元(前四周)作业总结 OO第一单元(前四周)作业总结要求(第四次作业) 0.前言 本次博客针对的是本人学习Java的第一阶段的三次作业的作业总结 第一次作业的内容是:7-1 计算税率 (20 ...

  3. 100行Python代码实现一款高精度免费OCR工具

    近期Github开源了一款基于Python开发.名为 Textshot 的截图工具,刚开源不到半个月已经500+Star. 很多人学习python,不知道从何学起.很多人学习python,掌握了基本语 ...

  4. Vue组件通信之父传子

    一般情况下,子组件中无法直接使用父组件的变量.借助子组件的props选项可以实现这一点. 这里我将一个vue实例作为一个父组件: const app = new Vue({ el:'#div1', d ...

  5. 【Java】AES加机解密工具类代码

    import java.security.NoSuchAlgorithmException; import java.security.SecureRandom; import javax.crypt ...

  6. Django-model模型中Field属性类别及选项

    参考:[Django官方文档] Django所使用模型中一些属性类别及选项(Field and Options) 1. Models Field 各种类型分别对应数据库中的各种类型,这是Django对 ...

  7. C#LeetCode刷题之#933-最近的请求次数(Number of Recent Calls)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4134 访问. 写一个 RecentCounter 类来计算最近的 ...

  8. 【Spring注解驱动开发】如何实现方法、构造器位置的自动装配?我这样回答让面试官很满意!

    在 冰河技术 微信公众号前面的文章中,我们介绍了如何使用注解来自动装配Spring组件.之前将的都是在来的字段上添加注解,那有没有什么方法可以实现方法.构造器位置的自动装配吗?今天我们就一起来探讨下如 ...

  9. Jmeter 常用函数(15)- 详解 __StringFromFile

    如果你想查看更多 Jmeter 常用函数可以在这篇文章找找哦 https://www.cnblogs.com/poloyy/p/13291704.htm 作用 从文本文件读取字符串,每次一行 需要注意 ...

  10. ethtool 设置网卡接收哈希

    检查 [root@localhost]# ethtool -n eth1 rx-flow-hash tcp4 TCP over IPV4 flows use these fields for comp ...