题目链接:

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. 洛谷 P2680 运输计划

    题目背景 公元 2044 年,人类进入了宇宙纪元. 题目描述 L 国有 n 个星球,还有 n-1 条双向航道,每条航道建立在两个星球之间,这 n-1 条航道连通了 L 国的所有星球. 小 P 掌管一家 ...

  2. Boxes And Balls(三叉哈夫曼编码)

    题目 原题链接:http://codeforces.com/problemset/problem/884/D 现有一堆小石子,要求按要求的数目分成N堆,分别为a1.a2....an.具体的,每次选一个 ...

  3. 1-3 编程基础 makefile工程管理

    GNU make Linux程序员必须学会使用GNU make来构建和管理自己的软件工程.GNU的make能够使整个工程的编译.链接只需要一个命令就可以完成. makefile make在执行时,需要 ...

  4. webpack打包 css文件里面图片路径 替换位置

    { test: /\.css$/, use: ExtractTextPlugin.extract({ use: ['css-loader?minimize', 'autoprefixer-loader ...

  5. docker 镜像仓库的安装与使用

    安装Docker Compose 解决依赖 [root@service-1 ~]# curl -L "https://github.com/docker/compose/releases/d ...

  6. 位(bit)、字节(byte)、字

    1.位(bit)来自英文bit,音译为“比特”,表示二进制位.位是计算机内部数据储存的最小单位,11010100是一个8位二进制数.一个二进制位只可以表示0和1两种状态(21):两个二进制位可以表示0 ...

  7. 监控java进程是否正常运行

    @echo off set _task=java.exe :checkstart for /f "tokens=1" %%n in ('tasklist ^| find " ...

  8. 硬盘写入 iso

    https://www.jb51.net/softjc/508796.html WinImage 正确操作是要有两个or以上-硬盘. 这样才能写入你要装的操作系统 测试或者安装

  9. git 设置ss代理

    git config --global https.proxy http://127.0.0.1:1080       git config --global https.proxy https:// ...

  10. vue的使用配置

    我的编辑器是webstorm,虽然占内容占资源, 但是用起来很方便, 刚开始接触的时候就是用这个软件,很喜欢. vue的教程 1.http://www.jianshu.com/p/5ba253651c ...