题意:

给定一个由n个整数组成的整数序列,可以滚动,滚动的意思就是前面k个数放到序列末尾去。问有几种滚动方法使得前面任意个数的和>=0.

思路:

先根据原来的数列求sum数组,找到最低点,然后再以最低点为始点,求解题目答案,(每求解一始点i,符合要求的条件为:sum[i]>=minx,[minx是i<x<=n中的最小值],之所以不用考虑前面的,就是因为我们的预处理是的所有的x<i的sum[x]的值满足sum[x]>=sum[i])

代码如下:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <stack>
#include <queue>
#include <vector>
#include <algorithm>
#include <set>
#include <map> #define M 1000005
#define mod 1000000007
#define INF 0x7fffffff
#define eps 1e-8
#define LL long long
#define LLU unsigned long long using namespace std; int a[M], n, mini;
LL sum[M], minx;
int main ()
{
while(scanf("%d", &n) && n)
{
minx = INF;
sum[0] = 0;
for(int i= 1; i <= n; ++i)
{
scanf("%d", &a[i]);
sum[i] = sum[i-1]+a[i];
if(minx>sum[i])
{
minx = sum[i];
mini = i;
}
}
sum[0] = 0;
int c = 0;
for(int i = mini+1; i <= n; ++i)
sum[++c] = sum[c-1]+a[i];
for(int i = 1; i <= mini; ++i)
sum[++c] = sum[c-1]+a[i];
minx = sum[n];
int ans = 0;
for(int i = n-1; i >= 0; --i)
{
if(sum[i]<=minx)
{
ans += 1;
minx = sum[i];
}
}
printf("%d\n", ans);
}
return 0;
}

  


单调队列的思路是比较简单的,

就是确定一个始点以后,然后在队列中找最小的值,满足要求的条件为:minx-sum[i]>=0

代码如下:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <stack>
#include <queue>
#include <vector>
#include <algorithm>
#include <set>
#include <map> #define M 1000005
#define mod 1000000007
#define INF 0x7fffffff
#define eps 1e-8
#define LL long long
#define LLU unsigned long long using namespace std;
int n, head, rear, a[M];
LL sum[2*M], deq[2*M];
void insert(int x)
{
while(head<=rear && sum[deq[rear]]>=sum[x]) --rear;
deq[++rear] = x;
}
LL push(int x)
{
while(deq[head]<=x-n) ++head;
return sum[deq[head]];
}
int main ()
{
while(scanf("%d", &n) && n)
{
head = 1; rear = 0;
for(int i = 1; i <= n; ++i)
scanf("%d", &a[i]);
sum[0] = 0;
for(int i = 1; i <= n; ++i)
sum[i] = sum[i-1]+a[i];
for(int i = n+1; i <= 2*n; ++i)
sum[i] = sum[i-1]+a[i-n];
for(int i = 1; i < n; ++i)
insert(i);
int ans = 0;
for(int i = n; i < 2*n; ++i)
{
insert(i);
if(push(i)-sum[i-n]>=0) ++ans;
}
printf("%d\n", ans);
}
return 0;
}

  

hdu 4193 - Non-negative Partial Sums(滚动数列)的更多相关文章

  1. HDU 4193 Non-negative Partial Sums(想法题,单调队列)

    HDU 4193 题意:给n个数字组成的序列(n <= 10^6).求该序列的循环同构序列中,有多少个序列的随意前i项和均大于或等于0. 思路: 这题看到数据规模认为仅仅能用最多O(nlogn) ...

  2. hdu 4193 Non-negative Partial Sums 单调队列。

    Non-negative Partial Sums Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/32768 K (Jav ...

  3. CodeForces 223C Partial Sums 多次前缀和

    Partial Sums 题解: 一个数列多次前缀和之后, 对于第i个数来说他的答案就是 ; i <= n; ++i){ ; j <= i; ++j){ b[i] = (b[i] + 1l ...

  4. 51nod1161 Partial Sums

    开始想的是O(n2logk)的算法但是显然会tle.看了解题报告然后就打表找起规律来.嘛是组合数嘛.时间复杂度是O(nlogn+n2)的 #include<cstdio> #include ...

  5. Non-negative Partial Sums(单调队列)

    Non-negative Partial Sums Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/32768 K (Jav ...

  6. TOJ 1721 Partial Sums

    Description Given a series of n numbers a1, a2, ..., an, the partial sum of the numbers is defined a ...

  7. 【计数】cf223C. Partial Sums

    考试时候遇到这种题只会找规律 You've got an array a, consisting of n integers. The array elements are indexed from ...

  8. 51 Nod 1161 Partial sums

    1161 Partial Sums  题目来源: CodeForces 基准时间限制:2 秒 空间限制:131072 KB 分值: 80 难度:5级算法题  收藏  取消关注 给出一个数组A,经过一次 ...

  9. CF思维联系–CodeForces - 223 C Partial Sums(组合数学的先线性递推)

    ACM思维题训练集合 You've got an array a, consisting of n integers. The array elements are indexed from 1 to ...

随机推荐

  1. wamp如何添加多个站点

    1.打开wamp目录下的bin目录下的apache/conf/extra/httpd-vhosts.conf文件(虚拟目录配置文件),修改文件:在num01下创建index.php文件,输出01,:在 ...

  2. Eclipse 配置 pyDev(转)

    转自:http://www.51testing.com/html/75/534775-859092.html 很久没有接触到Python了.以前都是在用2.7版本的,这次索性就安装3.2的了.一般的小 ...

  3. JS编写全选,复选按钮

    <!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8&qu ...

  4. export LD_LIBRARY_PATH=/opt/gtk/lib:$LD_LIBRARY_PATH

    如题,临时修改程序运行时动态库的搜索路径,平时经常会用到,记录之!

  5. Reset CSS:只选对的,不选"贵"的

    玉伯和正淳一起整理的一份 reset.css: /* KISSY CSS Reset 理念:清除和重置是紧密不可分的 特色:1.适应中文 2.基于最新主流浏览器 维护:玉伯(lifesinger@gm ...

  6. margin 碰到过的重叠问题

    双倍margin 稍微记录一下,因为常忘 float + margin,实际边距会变成设置值的双倍. 解决办法是 将元素设为 display:inline 最好要了解 IE的 haslayout 问题 ...

  7. c#输出、输入

    //输出 Console.WriteLine("这是一行文字");  自动回车的. Console.Write("Hello world");  不带回车的. ...

  8. 快快快!27个提升效率的iOS开源库推荐

    文章来源:http://www.csdn.net/article/2015-07-21/2825264-27-ios-open-source-libraries/1 我热爱开源,更喜爱那些花费宝贵的业 ...

  9. 关于myeclipse的破解的问题

    myeclipse的破解的问题,也是在网上down 了一下,发现并不需要找到什么注册的软件都可以自动完成的哦! 博客地址:http://blog.csdn.net/fuxiaohui/article/ ...

  10. [07]APUE:进程环境

    [a] exit / _Exit / _exit #include <stdlib.h> void exit(int status) void _Exit(int status) #inc ...