Non-negative Partial Sums

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

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 first 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 first 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
题解:给n个数,a0,a1,...an,求ai,ai+1,...an,a1,a2,...ai-1这样的排列种数,使得所有的前k(1<=k<=n)个的和都大于等于0;

求前缀和,加倍序列。

要满足前k个和都>=0,只需最小值>=0,所以用单调队列维护一个最小的前缀和sum[i],(i>=j-n+1),这样就保证了sum[j]-sum[i]最大,所以区间【j-n+1,i]最小。

维护一个单调队列代表终止位置的最小值从小到大;
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
const int MAXN = 1e6 + ;
int num[MAXN];
int sum[MAXN];
int q[MAXN];
int main(){
int n;
while(~scanf("%d", &n), n){
sum[] = ;
for(int i = ; i <= n; i++){
scanf("%d", num + i);
sum[i] = sum[i - ] + num[i];
}
for(int i = n + ; i <= *n; i++)
sum[i] = sum[i - ] + num[i - n];
// for(int i = 0; i <= 2*n; i++)
// printf("%d ", sum[i]);puts("");
int head = , tail = -, ans = ;
for(int i = ; i <= * n; i++){
while(head <= tail && sum[i] < sum[q[tail]])tail--;
q[++tail] = i;
// printf("i = %d %d %d\n", i, sum[q[head]], sum[i - n]);
if(i > n && sum[q[head]] - sum[i - n] >= )ans++;
while(head <= tail && q[head] <= i - n)head++;
}
printf("%d\n", ans);
}
return ;
}

Non-negative Partial Sums(单调队列)的更多相关文章

  1. hdu 4193 Non-negative Partial Sums 单调队列。

    Non-negative Partial Sums Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/32768 K (Jav ...

  2. 单调队列-hdu-4193-Non-negative Partial Sums

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4193 题目大意: 给n个数,a0,a1,...an,求ai,ai+1,...an,a1,a2,... ...

  3. HDU 4193 Non-negative Partial Sums(想法题,单调队列)

    HDU 4193 题意:给n个数字组成的序列(n <= 10^6).求该序列的循环同构序列中,有多少个序列的随意前i项和均大于或等于0. 思路: 这题看到数据规模认为仅仅能用最多O(nlogn) ...

  4. HDU 4193 Non-negative Partial Sums(单调队列)

     题目大意: 给定一个长度为n的循环序列.从n个不同位置開始,问有几个位置使得一下情况成立:全部前缀的和都大等于0(n <=1000000). 下午的训练赛.之前没学过单调队列所以用的线段树 ...

  5. HDU 4193 Non-negative Partial Sums【单调队列】

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4193 题意: 给定序列,可以把后面的连续的部分移到最前面来,问多少种移法使得最终得到的序列的前i项和 ...

  6. Parade(单调队列优化dp)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=2490 Parade Time Limit: 4000/2000 MS (Java/Others)    ...

  7. HDU 2490 Parade(DPの单调队列)(2008 Asia Regional Beijing)

    Description Panagola, The Lord of city F likes to parade very much. He always inspects his city in h ...

  8. CF372C Watching Fireworks is Fun(单调队列优化DP)

    A festival will be held in a town's main street. There are n sections in the main street. The sectio ...

  9. 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.  ...

随机推荐

  1. Android 自定义控件 优雅实现元素间的分割线 (支持3.0以下)

    转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/42407923 ,本文出自:[张鸿洋的博客] 1.概述 话说,随着Android ...

  2. 针对淡入淡出的定时轮播效果js

    如果不使用jquery的fadeIn和fadeOut的接口和不适用animate情况下,如果要做用js实现淡入淡出轮播效果,我所想到的办法就是使用css3新特性transition(注意好兼容性). ...

  3. JS+css滑动菜单简单实现

    JS+css滑动菜单 制作一个简单的滑动菜单,当鼠标指向菜单标题时,滑出二级菜单.移开时二级菜单隐藏.目标很简单,实践时有一些细节需要注意,比如鼠标移向二级菜单的 过程中,二级菜单消失了.还有定位出错 ...

  4. Lonely Integer

    https://www.hackerrank.com/challenges/lonely-integer def main(): n = int(raw_input()) s = dict() a = ...

  5. [模拟炉石](一)让游戏过程显示到cocos2d中

    在上篇中,如果运行了fireplace的tests/full_game.py,这个程序将一个游戏过程在终端上运行完成,可以看到整个过程,那么第一步要做的就是将这个过程显示到cocos2d创建的场景中去 ...

  6. Angry Professor

    def main(): t = int(raw_input()) for _ in range(t): n, k = map(int, raw_input().strip().split(' ')) ...

  7. cocos2dx ——屏幕适配

    本文出自 “夏天的风” 博客,请务必保留此出处 http://shahdza.blog.51cto.com/2410787/1550089 手机的屏幕大小千差万别,如现在流行的安卓手机屏幕大部分长宽比 ...

  8. 使用POI插件,提取导出excel的工具类

    在网站的不同的模块都需要使用到导入导出excel的功能,我们就需要写一个通用的工具类ExcelUtil. 我的思路:首先,导入和导出的Excel的文件格式固定:主标题,二级标题,数据行(姑且就这么叫) ...

  9. 论如何进CSDN博客排名前500

    http://www.jtahstu.com/blog/post-71.html 目前该方法并不适用于博客园,显然写博客园的程序员智商要高些.

  10. SpringMVC整合极光推送报错ClassNotFound

    问题: 今天在做后台和极光整合的过程中,将极光部分代码整合到Dao层,在启动项目的过程中总是报错,classNotFund cn/jpush/api/push/xxxx 极光官方文档: http:// ...