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. Python最全pdf学习书籍资料分享

    本人学习Python两年时间,期间统计了一些比较好的学习资料 1.基础资料  下载地址: 链接:https://pan.baidu.com/s/1sjtyYayBbQLsrUdaXWmzkg提取码:1 ...

  2. CI4框架应用二 - 项目目录

    我们之前搭建好了CI4的开发环境,下面我们来看一下CI4的目录结构. Administrator@PC- MINGW64 /c/wamp64/www/ci4 $ ls -l total drwxr-x ...

  3. CF1349F 【Slime and Sequences】part1

    由于本文过长,\(\LaTeX\) 炸了,分两篇,part2 题目描述 定义一个正整数序列为好序列,当且仅当如果某个数 \(k\) 出现过,那么一定有 \(k-1\) 在最后一个 \(k\) 的前面出 ...

  4. C++字符串转整形、浮点型stof()、atoi()、strtol()等

    头文件:#include<stdlib.h>string str;stof:float val=stof(str);atoi:int val=atoi(str);atol:long val ...

  5. Homekit_温湿度传感器

    本款产品为Homekit相关产品需要使用苹果手机进行操作,有兴趣的可以去以下链接购买: https://item.taobao.com/item.htm?spm=a1z10.1-c.w4004-112 ...

  6. python设计模式之享元模式

    python设计模式之享元模式 由于对象创建的开销,面向对象的系统可能会面临性能问题.性能问题通常在资源受限的嵌入式系统中出现,比如智能手机和平板电脑.大型复杂系统中也可能会出现同样的问题,因为要在其 ...

  7. 【AHOI 2015】 小岛 - 最短路

    描述 西伯利亚北部的寒地,坐落着由 N 个小岛组成的岛屿群,我们把这些小岛依次编号为 1 到 N . 起初,岛屿之间没有任何的航线.后来随着交通的发展,逐渐出现了一些连通两座小岛的航线.例如增加一条在 ...

  8. 30分钟闲置服务器建站(gitlab为例)

    前言 最近博主的阿里云主机又到了续费的时候了,刚买云主机的时候那是各种优惠各种打折,续费的时候只能当孙子了. 为了节省开支,又保证高性能的前提下,买了台10代NUC,内存和ssd自选,搭建一台个人服务 ...

  9. IP-master

    http://www.igotaobao.cn/IP-master/

  10. python基础 Day5

    python Day5 字典 其他数据类型的缺点 列表可以存储大量的数据,但是关联性不强. 列表的查询速度比较慢 其容器的数据类型为dict 其数据类型的分类 可变(不可哈希)的数据类型:list d ...