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 ...
随机推荐
- GridLayout自定义数字键盘(两个EditText)
功能说明: 适用于两个EditText输入框,并控制输入字符的长度,点击键盘中的"确定"按钮完成输入,点击"前一项"光标跳到前一个EditText 运行效果图如 ...
- Python模块:hashlib
hashlib:提供了常见的摘要算法,如MD5,SHA1等. import hashlib m = hashlib.md5() m.update('how to use md5 in python h ...
- jqueryValidation使用
jq form表单前端校验可以使用jq插件jquery-validation.js.具体的使用方法: 1.引入文件: <link rel="stylesheet" href= ...
- 用grunt进行前端工程化之路
我们的项目wecash4.0的前端构建考虑过用fis和grunt. 目录: 前期调研:fis vs grunt vs gulp? 一.安装grunt和项目. fis是百度fex研发的构建工具,非常方便 ...
- QT toLocal8Bit奇怪的问题
#include "mainwindow.h" #include "ui_mainwindow.h" #include<QDebug> MainWi ...
- 安装odoo服务
sysv init 服务 从 odoo 源码 debian 目录 拷贝 init 至 /etc/init.d/ 并 更名为 odoo cd /opt/odoo sudo cp /opt/odoo/de ...
- 使用 VisualCode + iTerm2 提交github的Pull Request
VisualCode集成github功能,是程序猿参与开源项目的利器.相比Sublime简单了很多(插件安装繁琐,比如你试试在Sublime2 安装gosublime,这里有坑; Sublime 3修 ...
- vi
e! 放弃所有修改,从上次保存文件开始再编辑 shift+g 最后一行 gg 第一行 u 恢复上一次操作 如果查找下一个,按"n"即可. set nu 显示行号 编辑模式下111g ...
- IE插件BHO
一丶接口IObjectWithSite //BHO项目(类库)添加引用两个COM //Microsoft HTML Object Library, Microsoft Internet Control ...
- 大前端学习笔记整理【六】this关键字详解
前言 在上一篇博客里我总结了下辨认this指向的四种方式,但是有师兄抛出一个问题来,我发现那些this的指向并不能说明更复杂的情况,先看下这段代码 var a = { name: 'a', getNa ...