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. 一道 3 行代码的 Python面试题,我懵逼了一天

    有意思的题目 题目:写出下面程序运行结果 很多人学习python,不知道从何学起.很多人学习python,掌握了基本语法过后,不知道在哪里寻找案例上手.很多已经做案例的人,却不知道如何去学习更加高深的 ...

  2. 【av68676164(p38-p40)】进程调度

    6.1 进程调度概念 进程调度 在合适的时候以一定策略选择一个就绪进程运行 进程调度的目标 响应速度尽可能快 进程处理的时间尽可能短 系统吞吐量尽可能大 资源利用率尽可能高 对所有进程要公平 避免饥饿 ...

  3. 比原链CTO James | Go语言成为区块链主流开发语言的四点理由

    11月24日,比原链CTO James参加了Go中国举办的Gopher Meetup杭州站活动,与来自阿里.网易的技术专家带来Kubernetes.区块链.日志采集.云原生等话题的分享.James向大 ...

  4. 全面介绍eBPF-概念

    全面介绍eBPF-概念 前面介绍了BCC可观测性和BCC网络,但对底层使用的eBPF的介绍相对较少,且官方欠缺对网络方面的介绍.下面对eBPF进行全面介绍. 目录 全面介绍eBPF-概念 BPF概述 ...

  5. 最火热的极速开发框架Spring Boot

    Spring Boot是Spring家族中的一个全新的框架,它用来简化Spring应用程序的创建和开发过程,也可以说Spring Boot能简化我们之前采用Spring mvc + Spring + ...

  6. java List接口一

    一 List接口概述 查阅API,看List的介绍.有序的 collection(也称为序列).此接口的用户可以对列表中每个元素的 插入位置进行精确地控制.用户可以根据元素的整数索引(在列表中的位置) ...

  7. java this关键字调用构造方法

    一 this调用构造方法 构造方法之间的调用,可以通过this关键字来完成. 格式: this(参数列表); 构造方法的调用举例: class Person { // Person的成员属性 priv ...

  8. excel如何写宏?如何用按钮?

    注:本次测试版本 excel版本2019 写宏? 准备工作(使用宏的一切前提)===========文件-选项-自定义功能区 (勾选开发工具) 开始写宏=======右击sheet1--查看代码    ...

  9. java多线程:线程同步synchronized(不同步的问题、队列与锁),死锁的产生和解决

    0.不同步的问题 并发的线程不安全问题: 多个线程同时操作同一个对象,如果控制不好,就会产生问题,叫做线程不安全. 我们来看三个比较经典的案例来说明线程不安全的问题. 0.1 订票问题 例如前面说过的 ...

  10. Promise 方法

    functionB(){ this.functionA() } functionA(){ return new Promise((resolve, reject) => { this.$http ...