hdu 4193 - Non-negative Partial Sums(滚动数列)
题意:
给定一个由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(滚动数列)的更多相关文章
- HDU 4193 Non-negative Partial Sums(想法题,单调队列)
HDU 4193 题意:给n个数字组成的序列(n <= 10^6).求该序列的循环同构序列中,有多少个序列的随意前i项和均大于或等于0. 思路: 这题看到数据规模认为仅仅能用最多O(nlogn) ...
- hdu 4193 Non-negative Partial Sums 单调队列。
Non-negative Partial Sums Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/32768 K (Jav ...
- CodeForces 223C Partial Sums 多次前缀和
Partial Sums 题解: 一个数列多次前缀和之后, 对于第i个数来说他的答案就是 ; i <= n; ++i){ ; j <= i; ++j){ b[i] = (b[i] + 1l ...
- 51nod1161 Partial Sums
开始想的是O(n2logk)的算法但是显然会tle.看了解题报告然后就打表找起规律来.嘛是组合数嘛.时间复杂度是O(nlogn+n2)的 #include<cstdio> #include ...
- Non-negative Partial Sums(单调队列)
Non-negative Partial Sums Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/32768 K (Jav ...
- TOJ 1721 Partial Sums
Description Given a series of n numbers a1, a2, ..., an, the partial sum of the numbers is defined a ...
- 【计数】cf223C. Partial Sums
考试时候遇到这种题只会找规律 You've got an array a, consisting of n integers. The array elements are indexed from ...
- 51 Nod 1161 Partial sums
1161 Partial Sums 题目来源: CodeForces 基准时间限制:2 秒 空间限制:131072 KB 分值: 80 难度:5级算法题 收藏 取消关注 给出一个数组A,经过一次 ...
- 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 ...
随机推荐
- Chrome 自动填充的表单是淡黄色的背景,有方法自定义吗
input:-webkit-autofill { -webkit-box-shadow: 0 0 0px 1000px white inset; }
- C# Winform学习---MDI窗体的设计,PictureBox控件(图片上一页下一页),Timer控件,MenuStrip控件
一.MDI窗体的设计 1.MDI简介 MDI(Multiple Document Interface)就是所谓的多文档界面,与此对应就有单文档界面 (SDI), 它是微软公司从Windows 2.0下 ...
- [刘阳Java]_避开环境配置快速的使用Java的开发工具_第5讲
我们一般学习Java都应该遵循通过系统的命令工具来编译Java程序,然后对编译好Java程序进行运行,这个是非常好的习惯.但是随着后期学习Java技术的深入我们也得像Java的IDE工具屈服.所以,可 ...
- Java动手动脑(二)
1>类的对象实例化 由于main为静态类型,所以在调用函数时也必须调用静态方法,如上代码中的求平方数的静态方法,如何在静态main中调用非静态类的方法呢? 静态方法只能直接访问静态成员,无法访问 ...
- python学习之——安装Beautifulsoup、requests、lxml
安装Beautiful soup: 1.下载安装包,解压到python的安装目录: 2.cmd 进入安装包解压后的存放位置: 3.使用命令:python setup.py build , pyt ...
- Docker学习<一>--初体验Windows环境下安装
背景 今天想试用spring boot与jwt协议的实现,配套就需要使用redis,但redis似乎windows环境版本部署起来不是那么舒心,果断尝试使用docker. 下载 下载地址: 稳定版:h ...
- c#遍历目录及子目录下某类11型的所有的文件
DirectoryInfo directory = new DirectoryInfo("D:\\aa\\"); FileInfo[] files = directory.GetF ...
- c# - catch(Exception ex) 会丢掉StackTrace 是怎么回事?
原本这篇文章就想写写StackTrace怎么会丢的问题, 但现在的内容变成了讨论怎么处理Exception的问题. 该不该用try catch, 什么时候用?也困扰了我很久, 好像随便写写就可以, 但 ...
- 一个login
login 1.获取提交表单,保存到变量中.2.判断用户密码是否正确,利用Model类.3.验证用户是否激活.3.判断用户是否记住登录状态,是的话,将其用cookie和session分别保存.没有的话 ...
- C# rename方法重命名文件
记得C# File类中是没有rename这个方法 所以网上很多都用的是move moveTo copy+delete等这些方法 其实以上的方法 虽然可以实现功能 但看起来总觉得很蛋疼 今天百度 突然发 ...