题意:

给定一个由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. .style, .getComputedStyle(),.currentStyle区别

    1)style只能获取行间样式(写在标签里面的):能读能写 2)currentStyle是专属ie的属性,区别他返回的是最终样式 及包括行间和外链css 3)getComputedStyle是一个可以 ...

  2. sendto频率过快导致发送丢包

    编写一个转发模块,虽然没有要求一转多时要达到多少路(不采用组播的情况下,单纯的一路转成多路),但是本着物尽其用的原则,尽可能测试一下极限. 网络环境:1000M,直连,多网卡 系统:Linux ver ...

  3. nginx php-fpm安装配置

    nginx本身不能处理PHP,它只是个web服务器,当接收到请求后,如果是php请求,则发给php解释器处理,并把结果返回给客户端. nginx一般是把请求发fastcgi管理进程处理,fascgi管 ...

  4. 第九章 springboot + mybatis + 多数据源 (AOP实现)(转载)

    本编博客转发自:http://www.cnblogs.com/java-zhao/p/5415896.html 1.ShopDao package com.xxx.firstboot.dao; imp ...

  5. SQLYog快捷键大全

    Ctrl+M   创建一个新的连接 Ctrl+N   使用当前设置新建连接 Ctrl+F4   断开当前连接 对象浏览器 F5   刷新对象浏览器(默认) Ctrl+B   设置焦点于对象浏览器 SQ ...

  6. vertx简单服务创建

    import java.util.HashMap;import java.util.Map; import org.slf4j.Logger;import org.slf4j.LoggerFactor ...

  7. GitHub安装配置

    GitHub安装配置1.安装Git-1.9.5-preview20141217 2.配置config文件Windows(在命令行下)cd /d %userprofile%if not exist &q ...

  8. 关于几种编码详解(Unicode,UTF-8,GB系列)

    最近学Python,老是被编码的问题搞得晕乎乎的,晚上看了好多篇博客,整理出来一个比较清晰的关于几种编码以及字符集的思路. 主要参考:http://blog.sina.com.cn/s/blog_6d ...

  9. firefox的console log功能

    http://www.ruanyifeng.com/blog/2011/03/firebug_console_tutorial.html Firebug是网页开发的利器,能够极大地提升工作效率. 但是 ...

  10. 寒假学习计划(c++作业2)

    C++学习计划 一.课程概况 1.课程名称:c++远征攻略 2.授课人姓名:james_yuan 3.课程链接地址:http://www.imooc.com/course/programdetail/ ...