题意:

给定一个由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. ASP.NET MVC4中@model使用多个类型实例的方法

    转http://blog.csdn.net/hulihui/article/details/48199897

  2. oracle启动脚本 .

        .#!/bin/bash set -x su -oracle >>EON lsnrctl start sqlplus /nolog >>EOF conn / as sy ...

  3. SAP 采购订单行项目中科目分配被隐藏,发现行项目设置中显示字段长度为0

    1.sm30 维护 视图 TCVIEW 修改对应字段的显示长度

  4. NPOI简单应用

    打开或创建文件 fs = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.ReadWrite); 不同版本的workbook if ...

  5. html导入css样式的方法

    在html网页中引入css样式表主要有一下四种方法 1.行内引入 <p style="width:100px;height:40px;color:red;"></ ...

  6. hdu 5876 (补图BFS) Sparse Graph

    题目:这里 题意: 相当于一开始给一个初始好了的无向完全图给你,然后给让你删除m条边,再给你一个点v,最后问你在剩下的图里从这个点v出发能到达所有边点的最小路径是多少? 一看是所有点的最小路径,一看就 ...

  7. 分享一个快速测试ios软件的工具

    简易IPA安装地址生成器 地址: https://www.neicexia.com/IPADistribute/Resources/index.html?fromsite#IPADistribute- ...

  8. 一次诡异的TOMCAT启动故障的解决

    该系统采用TOMCAT+SSH+Linux+Proxool连接池, 以前数据库是本地连接. 后换数据库远端连接,最近老是启动不了. 1.怀疑是proxool连接池没有自动断开后恢复.尝试解决,不是这个 ...

  9. .NET 4.0 中超长路径超长文件名的问题

    1. 昨天开发中遇到一个问题,场景如下: 客户端从服务器下载一个文件并解压,客户端在使用后需要做清理操作(删除解压出来的文件),结果删除失败,抛出如下异常: The specified path, f ...

  10. C# 并行编程 之 轻量级手动重置事件的使用

    目录(?)[-] 简单介绍 使用超时和取消 跨进程或AppDomain的同步   简单介绍 如果预计操作的等待的时间非常短,可以考虑使用轻量级的手动重置事件,ManualResetEventSlim. ...