Non-negative Partial Sums HDU
来源:互联网 发布:php抓取网页指定内容 编辑:程序博客网 时间:2024/06/16 21:10
题目链接:点我
You are given a sequence of n numbers a 0,..., a n-1. A cyclic shift by k positions (0<=k<=n-1) results in the following sequence: a k a k+1,..., a n-1, a 0, a 1,..., a k-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<=10 6), the number of integers in the sequence. The second contains n integers a 0,..., a n-1 (-1000<=a i<=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
题意:
给你一个循环序列,求序列中满足每一项的前缀和都大于等于0的序列的个数.
思路:
序列倍增然后求前缀和,然后维护一个前缀和单调递增的单调队列,
代码:
#include<cstdio>#include<algorithm>#include<cstring>#include<iostream>#include<cmath>using namespace std;const int maxn = 2e6 + 10;int sum[maxn];int a[maxn];int main(){ int n; while(scanf("%d", &n), n){ for(int i = 1;i <= n; ++i){ scanf("%d", &sum[i]); sum[n+i] = sum[i]; sum[i] += sum[i-1]; }int ans = 0; for(int i = n+1; i < n+n; ++i){ sum[i] = sum[i-1] + sum[i]; }int l =1,r = 0; for(int i = 1; i < n+n; ++i){ while(r>=l && i - a[l]-1>= n) ++l; while(r >= l && sum[a[r]] >= sum[i]) --r; a[++r] = i; if( i >= n && sum[a[l]] - sum[i-n] >=0) ++ans; } printf("%d\n",ans); } return 0;}
阅读全文
0 0
- Non-negative Partial Sums HDU
- hdu Non-negative Partial Sums(单调队列)
- hdu 4193 Non-negative Partial Sums
- HDU 4193 Non-negative Partial Sums
- HDU-4193 Non-negative Partial Sums
- Non-negative Partial Sums
- HDU 4193 Non-negative Partial Sums(单调队列)
- HDU OJ 4193 Non-negative Partial Sums 【单调队列】
- HDU-4193 Non-negative Partial Sums 单调队列
- hdu 4193 - Non-negative Partial Sums(滚动数列)
- 单调队列-hdu-4193-Non-negative Partial Sums
- 单调队列—— HDU 4193 Non-negative Partial Sums
- 【单调队列】hdu 4193 Non-negative Partial Sums
- HDU 1193 Non-negative Partial Sums / 单调队列
- hdu 4193 Non-negative Partial Sums 单调队列
- HDU Non-negative Partial Sums (单调队列)
- HDU 4193 Non-negative Partial Sums(单调队列)
- HDU 4193 Non-negative Partial Sums【单调队列】
- 横向导航条页面居中的方法
- 线程通信常用方法
- IntelliJ Idea常用的快捷键
- HDU 1222 A
- 机器学习实战(第2章 k-近邻算法)
- Non-negative Partial Sums HDU
- 微信支付:curl出错,错误码:60
- Mybatis打印Sql到控制台
- 使用SpringMVC时无参的重定向方法
- BP反向传播算法的具体计算
- px+em+pt+rpx+rem的区别
- 那些数据类型导致的坑
- Zookeeper选举过程
- c++继承