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. 记不住git命令?试试这个命令浏览网站

    Find the right git commands without digging through the web. 找 git 命令,无需谷歌百度,无需 git -help,这个网站以图形界面的 ...

  2. 使用opencv在Qt控件上播放mp4文件

    文章目录 简介 核心代码 运行结果 简介 opencv是一个开源计算机视觉库,功能非常多,这里简单介绍一下OpenCV解码播放Mp4文件,并将图像显示到Qt的QLabel上面. 核心代码 头文件 #i ...

  3. 有用的20个Python代码段

    Python是一种非BS编程语言.设计简单和易读性是它广受欢迎的两大原因.正如Python的宗旨:美丽胜于丑陋,显式胜于隐式. 记住一些帮助提高编码设计的常用小诀窍是有用的.在必要时刻,这些小诀窍能够 ...

  4. Java常用类:包装类,String,日期类,Math,File,枚举类

    Java常用类:包装类,String,日期类,Math,File,枚举类

  5. Ubuntu用户都应该了解的快捷键

    无论我们使用什么操作系统还是什么软件,快捷键都是非常有用的,因为可以在启动应用程序或跳转到所需窗口,可以快速进行很多操作,而无需动鼠标到处点,节省时间和精力,提高效率. 就像在Windows中一样,U ...

  6. 洛谷P3513 [POI2011]KON-Conspiracy

    洛谷P3513 [POI2011]KON-Conspiracy 题目描述 Byteotia的领土被占领了,国王Byteasar正在打算组织秘密抵抗运动. 国王需要选一些人来进行这场运动,而这些人被分为 ...

  7. java 接口一

    一 接口的概念 接口是功能的集合,同样可看做是一种数据类型,是比抽象类更为抽象的”类”. 接口只描述所应该具备的方法,并没有具体实现,具体的实现由接口的实现类(相当于接口 的子类)来完成.这样将功能的 ...

  8. C#LeetCode刷题之#628-三个数的最大乘积( Maximum Product of Three Numbers)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3726 访问. 给定一个整型数组,在数组中找出由三个数组成的最大乘 ...

  9. MySQL设置跳过密码验证

    1.linux系统下 在/etc/my.cnf文件中, [mysqld]下面新增skip-grant-tables,然后重启服务器.

  10. 在Linux使用虚拟环境

    定义 “虚拟环境”,是python解释器的一个私有副本.在这个环境中,你可以安装私有包,而且不会影响系统中安装的全局python解释器. 作用 为每个程序单独创建虚拟环境时,可以保证程序只能访问虚拟环 ...