Non-negative Partial Sums

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1420    Accepted Submission(s): 544

Problem Description
You are given a sequence of n numbers a0,..., an-1. A cyclic shift by k positions (0<=k<=n-1) results in the following sequence: ak ak+1,..., an-1, a0, a1,..., ak-1. How many of the n cyclic shifts satisfy the condition that the sum of the fi rst i numbers is greater than or equal to zero for all i with 1<=i<=n?
 
Input
Each test case consists of two lines. The fi rst contains the number n (1<=n<=106), the number of integers in the sequence. The second contains n integers a0,..., an-1 (-1000<=ai<=1000) representing the sequence of numbers. The input will finish with a line containing 0.
 
Output
For each test case, print one line with the number of cyclic shifts of the given sequence which satisfy the condition stated above.
 
Sample Input
3
2 2 1
3
-1 1 1
1
-1
0
 
Sample Output
3
2
0
 
Source
 
Recommend
lcy   |   We have carefully selected several similar problems for you:  4190 4192 4187 4188 4189 
 
 
 /*
题意:
刚刚又一道hdu的题目;
题意:10^6个数字,有10^6种形式。
如 a b c d , b c d a , c d a b, d a b c;
统计在所以情况中如果满足任意前i数和都>=0 的个数。
想了一下,思路有了。可以这样子。
任意前i数和都满足>=0,那么最小的是不是也就满足了呢?
肯定的。所以。题目可以转换为
以i为开头,长度为n的前提下,求最小值。
这样的话,只有q[head].sum - s[ i - n]; */ #include<iostream>
#include<stdio.h>
#include<cstring>
#include<cstdlib>
using namespace std; int a[];
int s[];
typedef struct
{
int num;
int sum;
}Queue;
Queue tmp,q[]; int main()
{
int n,i,len,Num,tom;
int head,tail;
while(scanf("%d",&n)>)
{
if(n==)break;
for(i=;i<=n;i++)
scanf("%d",&a[i]);
len=n*;
for(i=n+;i<=len;i++)
a[i]=a[i-n];
for(s[]=,i=;i<=len;i++)
s[i]=s[i-]+a[i];
head=;tail= -; Num=; tom=;
for(i=;i<=len;i++)
{
tmp.num=++Num;
tmp.sum=s[i];
while( head<=tail && q[tail].sum>tmp.sum ) tail --;
q[++tail]=tmp;
if( i>n )
{
while( head<=tail && q[head].num+n<=i ) head++;
if( q[head].sum-s[i-n]>=) tom++;
}
}
printf("%d\n",tom);
}
return ;
}

hdu 4193 Non-negative Partial Sums 单调队列。的更多相关文章

  1. hdu 3706 Second My Problem First 单调队列

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3706 Second My Problem First Time Limit: 12000/4000 M ...

  2. HDU 4122 Alice's mooncake shop 单调队列优化dp

    Alice's mooncake shop Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem ...

  3. hdu 4122 Alice's mooncake shop(单调队列)

    题目链接:hdu 4122 Alice's mooncake shop 题意: 有n个订单和可以在m小时内制作月饼 接下来是n个订单的信息:需要在mon月,d日,year年,h小时交付订单r个月饼 接 ...

  4. hdu 3415 Max Sum of Max-K-sub-sequence(单调队列)

    题目链接:hdu 3415 Max Sum of Max-K-sub-sequence 题意: 给你一串形成环的数,让你找一段长度不大于k的子段使得和最大. 题解: 我们先把头和尾拼起来,令前i个数的 ...

  5. HDU 6444 Neko's loop(单调队列)

    Neko has a loop of size nn. The loop has a happy value aiai on the i−th(0≤i≤n−1)i−th(0≤i≤n−1) grid.  ...

  6. HDU 4123(树的直径+单调队列)

    Bob’s Race Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  7. 【HDU】3401:Trade【单调队列优化DP】

    Trade Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...

  8. ACM学习历程—HDU 5289 Assignment(线段树 || RMQ || 单调队列)

    Problem Description Tom owns a company and he is the boss. There are n staffs which are numbered fro ...

  9. HDU 5945 Fxx and game (DP+单调队列)

    题意:给定一个 x, k, t,你有两种操作,一种是 x - i (0 <= i <= t),另一种是 x / k  (x % k == 0).问你把x变成1需要的最少操作. 析:这肯定是 ...

随机推荐

  1. JDBC技术(汇聚页)

    JDBC代表Java数据库连接(Java Database Connectivity),它是用于Java编程语言和数据库之间的数据库无关连接的标准Java API, 换句话说:JDBC是用于在Java ...

  2. 面向对象之ajax

    1.Ajax发送请求的几个步骤 1. 创建 XMLHttpRequest 对象 var xhr = new XMLHttpRequest();//IE6 使用var xhr= new ActiveXO ...

  3. UVA10140 Prime Distance

    UVA10140 Prime Distance 给定两个整数L,R(1<=L<=R<=2^{31},R-L<=10^6)L,R(1<=L<=R<=231,R− ...

  4. 后序线索化二叉树(Java版)

    前面介绍了前序线索化二叉树.中序线索化二叉树,本文将介绍后序线索化二叉树.之所以用单独的一篇文章来分析后序线索化二叉树,是因为后序线索化二叉树比前序.中序要复杂一些:另外在复习线索化二叉树的过程中,大 ...

  5. shell-005:备份。

    #  这个案例感觉有点绕,如果是本地机器备份完全没必要.下面带颜色的写法值得我们借鉴,所以还是做下笔记吧 #!/bin/bash # 找出//目录下所有txt结尾的文件,且形成一个列表清单的文件 fi ...

  6. Oracle数据库学习(三):Oracle数据库的环境配置与安装

    1.解压oracle安装包 将oracle的安装包和补丁包都存放在/home/oracle/soft目录下,然后对其进行解压,建议以root权限进行解压. unzip p13390677_112040 ...

  7. P3348 [ZJOI2016]大森林(Link-cut-tree)

    传送门 题解 题面大意: \(0.\)区间加节点 \(1.\)区间换根 \(2.\)单点询问距离 如果没有\(1\)操作,因为区间加节点都是加在下面,所以我们可以直接把\(n\)棵树压成一棵树,直接询 ...

  8. springboot(十三)-分库分表-手动配置

    sharding-jdbc简介 Sharding-JDBC直接封装JDBC API,可以理解为增强版的JDBC驱动,旧代码迁移成本几乎为零: 可适用于任何基于java的ORM框架,如:JPA, Hib ...

  9. swagger注释API详细说明

    API详细说明 注释汇总 @RequestMapping此注解的推荐配置 value method produces 示例: @ApiOperation("信息软删除") @Api ...

  10. spring定时任务详解

    (一)在spring.xml里加入task的命名空间 xmlns:task="http://www.springframework.org/schema/task" http:// ...