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. django rest framework实现分页功能

    在web开发中很多需求都需要实现分页功能,然而 Django Rest Framework 自带的分页功能,只能在 mixins.ListModelMixin and generics.Generic ...

  2. JSP里面九个内置对象

    JSP内置对象(9个常用的内置对象) 1.request对象 客户端的请求信息被封装在request对象中,通过它才能了解到客户的需求, 然后做出响应.它是HttpServletRequest类的实例 ...

  3. SAE实践——用SVN命令行同步/提交代码

    1. 同步应用到本地 注:首次使用svn需要输入安全认证密码 在终端输入以下命令 svn co https://svn.sinaapp.com/nyhello nyhello替换为自己的应用名称. 用 ...

  4. 自适应大邻域搜索代码系列之(1) - 使用ALNS代码框架求解TSP问题

    前言 上次出了邻域搜索的各种概念科普,尤其是LNS和ALNS的具体过程更是描述得一清二楚.不知道你萌都懂了吗?小编相信大家早就get到啦.不过有个别不愿意透露姓名的热心网友表示上次没有代码,遂不过瘾啊 ...

  5. C#-WebForm-AJAX阿贾克斯(一)基础知识

    AJAX 即“ Asynchronous Javascript And XML ”(异步JavaScript和XML),是指一种创建交互式网页应用的网页开发技术. AJAX = 异步 JavaScri ...

  6. SPI 用户空间的读写操作

    spi_device 虽然用户空间不需要直接用到spi_device结构体,但是这个结构体和用户空间的程序有密切的关系,理解它的成员有助于理解SPI设备节点的IOCTL命令,所以首先来介绍它.在内核中 ...

  7. elment 中 el-table 进行校验

    脑洞大开:什么是展示数据最好的方式呢,表格,写得又快,又清晰,又明显,那么就积累一些工作中表格经常使用到的东西. 第一步:效果图: 第二步:举个例子: <template> <div ...

  8. python实现计算器功能

    import re def strip_operate(exp): # 合并多余的操作符 exp = exp.replace("+-", "-") exp = ...

  9. hdu1024 Max Sum Plus Plus 滚动dp

    Max Sum Plus Plus Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  10. (转)MySQL高可用架构之MHA

    MySQL高可用架构之MHA  原文:http://www.cnblogs.com/gomysql/p/3675429.html 简介: MHA(Master High Availability)目前 ...