hdu 4193 单调队列
来源:互联网 发布:美国历年财政支出数据 编辑:程序博客网 时间:2024/06/14 02:04
题目:
Non-negative Partial Sums
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 2849 Accepted Submission(s): 977
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
32 2 13-1 1 11-10
Sample Output
320
Source
SWERC 2011
分析:
环的问题可以通过开双倍数组并将输入数据复制两份接起来来解决
[i,j]的每一个前缀和都大于0等价于从数列起点开始的前缀和在[I,J]部分的最小值都满足比sum(i-1)大
问题转化为高效求[i,j]区间的最小值 即固定区间求最值 联想到单调队列,相当于一个定长的滑块往后滑,滑的过程中通过入队出队操作使得队首值即为最小值
代码:
#include <bits/stdc++.h>using namespace std;const int maxn=1000010<<1;int sum[maxn],q[maxn];int n,cnt,Front,rear;//队列中维护[1,2n]的下标 使得这些下标对应的前缀和递增 这样队首元素对应的前缀和是最小的inline void in(int i) { while(Front<=rear&&sum[q[rear]]>sum[i]) rear--;//保持前缀和严格递增 或者不降也行 q[++rear]=i;//插到队尾}inline void out(int i,int n) {//删除i使得队列大小为n if(q[Front]<=i-n) Front++;//i-n属于[0,n) 从队首移除}int main(){//1528MS9548K while (scanf("%d",&n)==1 && n) { sum[0]=0; cnt=0; Front=0; rear=-1; for(int i=1;i<=n;++i) { scanf("%d",&sum[i]); sum[i+n]=sum[i]; } for (int i=2;i<n+n;++i) sum[i]+=sum[i-1]; for(int i=1;i<n;++i) in(i); //现在队列里面有n-1个元素 后面每进一个就要出一个 for(int i=n;i<n+n;++i){ in(i); out(i,n); if(sum[q[Front]]-sum[i-n]>=0) cnt++; } printf("%d\n",cnt); } return 0;}
阅读全文
0 0
- hdu 4193#单调队列
- HDU 4193 单调队列
- HDU 4193单调队列
- hdu 4193(单调队列)
- hdu 4193 单调队列
- hdu 4193(单调队列)
- hdu 4193 单调队列
- hdu 4193(单调队列)
- HDU-4193 (单调队列)
- hdu 1506#单调队列
- HDU 3415 单调队列
- HDU 3530 单调队列
- HDU 3415单调队列
- hdu 3415 单调队列
- HDU 3530 单调队列
- hdu 1506 单调队列
- hdu 3415 单调队列
- hdu 3410(单调队列)
- activemq 和webservice 的区别
- 【0049】SQL数据定义语言--表约束
- matplotlib学习—绘制简单自定义曲线
- 【大数据部落】R在GIS中用ggmap地理空间数据分析
- [汇总信息] Laravel 上使用 phpexcel的两种方式
- hdu 4193 单调队列
- Java高级开发工程师面试
- 【大数据部落】r语言618电商大数据分析可视化报告
- 通用的事件侦听器函数
- 工作笔记-新系统安装deb文件失败
- SpringMvc实现文件的上传
- C++ helloworld
- kubeadm安装kubernetes(calico)
- 访问网页的各种http状态码的解释说明