hdu 4193 Non-negative Partial Sums
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=4193
题意:给出一个n数列,要求把前i(1<=i<=n)个数移到剩余数列的后面形成新的数列,如果新数列满足前i(1<=i<=n)个数均大于等于0,算一种情况,问总共有多少种情况。
简单思路:单调队列+前缀和,枚举每一个可能的以i为首的数列,用单调队列找出区间[i,i+n-1]的最小前缀和,如果最小前缀和sum[k]-sum[i-1]>=0,就可能算一种,并在枚举的时候更新单调队列。
总结:1.昨天比赛训练的时候没有想到单调队列的做法,简单的前缀和果然的超时了,学过的算法及思维远远不够,因为貌似这道题去年我做过,囧。。。
2.如果单调队列加上结构体得2000+ms,我的挫代码就如此,菜。应该就在前缀和数组sum[]上面进行单调会快一点吧。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<queue>
#define inf 0x7fffffff
using namespace std;
const int maxn=+;
struct node
{
int value;
int tag;
node () {tag=;value=; }
}q[maxn*];
int sum[maxn*];
int an[maxn*];
int main()
{
int n; while (cin>>n,n)
{
memset(sum,,sizeof(sum));
for (int i= ;i<=n ;i++)
{
scanf("%d",&an[i]);
an[n+i]=an[i];
}
for (int i= ;i<=*n ;i++) sum[i]=sum[i-]+an[i];
int h=,r=;
for (int i= ;i<n ;i++)
{
while (h<=r && q[r].value>=sum[i]) r--;
q[++r].value=sum[i];
q[r].tag=i;
}
int count=;
for (int i=n ;i<*n ;i++)
{
while (h<=r && q[r].value>=sum[i]) r--;
q[++r].value=sum[i];
q[r].tag=i; while (h<=r && q[h].tag<i-n+) h++;
if (q[h].value-sum[i-n] >=) count++;
}
cout<<count<<endl;
}
return ;
}
hdu 4193 Non-negative Partial Sums的更多相关文章
- hdu 4193 Non-negative Partial Sums 单调队列。
Non-negative Partial Sums Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/32768 K (Jav ...
- HDU 4193 Non-negative Partial Sums(想法题,单调队列)
HDU 4193 题意:给n个数字组成的序列(n <= 10^6).求该序列的循环同构序列中,有多少个序列的随意前i项和均大于或等于0. 思路: 这题看到数据规模认为仅仅能用最多O(nlogn) ...
- 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 ...
- CodeForces 223C Partial Sums 多次前缀和
Partial Sums 题解: 一个数列多次前缀和之后, 对于第i个数来说他的答案就是 ; i <= n; ++i){ ; j <= i; ++j){ b[i] = (b[i] + 1l ...
- 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 ...
随机推荐
- partial class 说明
C# 2.0 可以将类.结构或接口的定义拆分到两个或多个源文件中,在类声明前添加partial关键字即可. 例如:下面的PartialTest类 class PartialTest { string ...
- DataGridView取消默认选中行
DataGridView在添加数据后会默认选中第 一个单元格或者第一行,我就想取消它的默认选中行.在DataGridView绑定数据之后加上了ClearSelection().这样一来,不论是启动窗体 ...
- 【原创】解决鼠标经过子元素触发mouseout,mouseover事件的问题
关键词:父子元素关系 mouseout mouseover 事件 事件冒泡 初期代码: <!DOCTYPE html> <html> <head> < ...
- PHP MYSQLI中事务处理
MYSQL事务处理让所有sql语句执行成功后才去处理,如果有一条没有成功或者报错就会回滚事务,防止敏感操作处理失败.MYSQL中只有INNODB和BDB类型的数据表才能支持事务处理!其它类型是不支持的 ...
- [.ashx檔?泛型处理例程?]基础入门#1....能否用中文教会我?别说火星文?
原文出處 http://www.dotblogs.com.tw/mis2000lab/archive/2013/08/20/ashx_beginner_01.aspx [.ashx檔?泛型处理例程? ...
- web.config的奇淫巧技
<connectionStrings configSource="db.config"/> 外部文件db.config: <connectionStrings&g ...
- 16.python中的浅拷贝和深拷贝
在讲什么是深浅拷贝之前,我们先来看这样一个现象: a = ['scolia', 123, [], ] b = a[:] b[2].append(666) print a print b
- Scut游戏服务器免费开源框架--快速开发(2)
Scut快速开发(2) Python脚本开发 1 开发环境 Scut Lib版本:5.2.3.2 需要安装的软件 a) IIS和消息队列(MSMQ) 进入控制面板,程序和功能 b) ...
- video 测试
https://segmentfault.com/a/1190000002401961 音量调节https://www.google.com/?gws_rd=ssl#newwindow=1& ...
- DB2行转列(多维度)
多维度下进行行列转换,下面的行列转换时根据客户,所属银行机构进行的行列转换. -----------------建表 CREATE TABLE CUST_BANK_INFO ( CUST_ID ), ...