题目链接:

http://acm.hdu.edu.cn/showproblem.php?pid=4193

题意:

给定序列,可以把后面的连续的部分移到最前面来,问多少种移法使得最终得到的序列的前i项和均大于等于0(1≤i≤n)?

分析:

先将数组前后拼接起来,记录每个位置的前缀和。然后去找长度为n的滑动窗口,在这个窗口内,要保证前i项和大于等于0,也即区间[i,n+i]之间的找到的最小的前缀和要比a[i−1]大。而不涉及修改,这个最小前缀和的获取和维护就利用单调队列即可~

代码:

#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
const int maxn = 2e6 + 5;
int q[maxn], a[maxn];
int head, rear;
int n, cnt;
void inque(int i)
{
while(rear >= head && a[q[rear]] >= a[i]) rear--;
q[++rear] = i;
}
void outque(int i)
{
if(q[head] < i - n + 1){
head++;
}
}
int main (void)
{
while(scanf("%d", &n) && n){
head = 0, rear = 0;
memset(a, 0, sizeof(a));
memset(q, 0, sizeof(q));
for(int i = 0; i < n; i++){
scanf("%d", &a[i]);
a[n + i] = a[i];
}
for(int i = 1; i < 2 * n; i++)
a[i] += a[i - 1];
for(int i = 0; i < n; i++)
inque(i);
int cnt = 0;
for(int i = n; i < 2 * n; i++){
outque(i);
inque(i);
if(a[q[head]] - a[i- n] >= 0) cnt++;
}
printf("%d\n", cnt);
}
return 0;
}
/*
5
-1 -2 -3 -4 -5
*/

HDU 4193 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 3706 Second My Problem First 单调队列

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. Android如何用阿里云的API进行身份证识别

    准备工作:在libs下添加 alicloud-Android-apigateway-sdk-1.0.1.jar,commons-codec-1.10-1.jar 在build.gradle添加  co ...

  2. Understanding Scroll Views 深入理解 scroll view 读书笔记

    Understanding Scroll Views 深入理解 scroll view  读书笔记   It may be hard to believe, but a UIScrollView is ...

  3. 用JS获取Html中所有图片文件流然后替换原有链接

    function displayHtmlWithImageStream(bodyHtml) { var imgReg = /<img.*?(?:>|\/>)/gi; var arr ...

  4. 8.2.6 PEB —— PEB结构值不正确的问题

    书中作者使用 dt _PEB xxxxxx 命令来查看当前进程的PEB结构. 实际操作后PEB结构显示的成员值: 作为进程链表的LDR结构居然没有值,这显然是不正常的,地址也没有输错,问题到底出在哪里 ...

  5. 简单修改BOOK主题样式

    body{ font-size: 13px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; margin: 0px; word ...

  6. Java数据结构和算法(四)--链表

    日常开发中,数组和集合使用的很多,而数组的无序插入和删除效率都是偏低的,这点在学习ArrayList源码的时候就知道了,因为需要把要 插入索引后面的所以元素全部后移一位. 而本文会详细讲解链表,可以解 ...

  7. tabs标签页的数据缓存

    一进入tabs标签页默认就将所有标签页的数据请求到,并渲染到页面上, 这样如果数据量太大的话会渲染很久, 我的需求就是点击不同的标签时再请求数据,同时对点击过的标签页数据进行缓存,下次点击时不再重新请 ...

  8. TCP三次握手简单理解

  9. js 对象细节

    原型和原型链 在对象自身身上找不到指定属性时,就会到这个对象的原型__proto__上找,原型也是指向一个对象,在这个对象上还找不到对应属性,则继续到原型上来找...以上过程形成原型链. 访问对象的原 ...

  10. CSS3---圆角设置

    1.border-radius是向元素添加圆角边框.border-radius:10px; /* 所有角都使用半径为10px的圆角 */     border-radius: 5px 4px 3px ...