HDU 4193 Non-negative Partial Sums【单调队列】
题目链接:
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【单调队列】的更多相关文章
- hdu 4193 Non-negative Partial Sums 单调队列。
Non-negative Partial Sums Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/32768 K (Jav ...
- hdu 3706 Second My Problem First 单调队列
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3706 Second My Problem First Time Limit: 12000/4000 M ...
- 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 ...
- hdu 4122 Alice's mooncake shop(单调队列)
题目链接:hdu 4122 Alice's mooncake shop 题意: 有n个订单和可以在m小时内制作月饼 接下来是n个订单的信息:需要在mon月,d日,year年,h小时交付订单r个月饼 接 ...
- hdu 3415 Max Sum of Max-K-sub-sequence(单调队列)
题目链接:hdu 3415 Max Sum of Max-K-sub-sequence 题意: 给你一串形成环的数,让你找一段长度不大于k的子段使得和最大. 题解: 我们先把头和尾拼起来,令前i个数的 ...
- 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. ...
- HDU 4123(树的直径+单调队列)
Bob’s Race Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- 【HDU】3401:Trade【单调队列优化DP】
Trade Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submi ...
- ACM学习历程—HDU 5289 Assignment(线段树 || RMQ || 单调队列)
Problem Description Tom owns a company and he is the boss. There are n staffs which are numbered fro ...
- HDU 5945 Fxx and game (DP+单调队列)
题意:给定一个 x, k, t,你有两种操作,一种是 x - i (0 <= i <= t),另一种是 x / k (x % k == 0).问你把x变成1需要的最少操作. 析:这肯定是 ...
随机推荐
- js 输出某年某月某日的天数/判断闰年
console.log(getDays(2017,12,12)); function getDays(year,month,day){ var arr = [31,28,31,30,31,30,31, ...
- iview upload on-format-error 事件 在 before-upload 事件 之后,导致在before里面阻止上传后,监测事件失效,需要自己手工写
iview upload on-format-error 事件 在 before-upload 事件 之后,导致在before里面阻止上传后,监测事件失效,需要自己手工写
- 一篇文章搞懂高级程序员、架构师、技术总监、CTO从薪资到技能的区别
一篇文章搞懂高级程序员.架构师.技术总监.CTO从薪资到技能的区别 http://youzhixueyuan.com/senior-programmers-architects-technical-d ...
- Java以组的数量将字符串分组
package org.jimmy.autosearch2019.test; import java.util.ArrayList; public class Test20190327 { publi ...
- 暑假集训 || 区间DP
区间DP 经典石子合并问题V1 复杂度 On3 int a[SZ], sum[SZ], f[SZ][SZ]; int main() { int n; scanf("%d", ...
- HTML基础(五)表单
表单的工作原理 简单来说就是客户在浏览器输入信息之后,浏览器将用户在表单中的数据进行打包发送给服务器,服务器接收到之后进行处理,如下图 语法 <form> 表单元素</form> ...
- ios之UITableView
今天要分享的是IOS开发中一个使用率非常高的一个控件-------UITableView,这两天正在使用tableview做信息的显示,在写代码时对tableview和tableviewcell的几种 ...
- 记录一个scrapy的坑
在win10上用命令 pip install scrapy 安装scrapy的时候提示: Could not find a version that satisfies the requirement ...
- LeetCode 翻转字符串里的单词
给定一个字符串,逐个翻转字符串中的每个单词. 示例 1: 输入: "the sky is blue" 输出: "blue is sky the" 示例 2: 输 ...
- mysql查询表中最小可用id值
今天在看实验室的项目时,碰到的一个问题,.先把sql语句扔出来 // 这条语句在id没有1时,不能得到正确的查询结果. select min(id+1) from oslist c where not ...