题目地址: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的更多相关文章

  1. hdu 4193 Non-negative Partial Sums 单调队列。

    Non-negative Partial Sums Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/32768 K (Jav ...

  2. HDU 4193 Non-negative Partial Sums(想法题,单调队列)

    HDU 4193 题意:给n个数字组成的序列(n <= 10^6).求该序列的循环同构序列中,有多少个序列的随意前i项和均大于或等于0. 思路: 这题看到数据规模认为仅仅能用最多O(nlogn) ...

  3. 51nod1161 Partial Sums

    开始想的是O(n2logk)的算法但是显然会tle.看了解题报告然后就打表找起规律来.嘛是组合数嘛.时间复杂度是O(nlogn+n2)的 #include<cstdio> #include ...

  4. Non-negative Partial Sums(单调队列)

    Non-negative Partial Sums Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/32768 K (Jav ...

  5. TOJ 1721 Partial Sums

    Description Given a series of n numbers a1, a2, ..., an, the partial sum of the numbers is defined a ...

  6. 【计数】cf223C. Partial Sums

    考试时候遇到这种题只会找规律 You've got an array a, consisting of n integers. The array elements are indexed from ...

  7. CodeForces 223C Partial Sums 多次前缀和

    Partial Sums 题解: 一个数列多次前缀和之后, 对于第i个数来说他的答案就是 ; i <= n; ++i){ ; j <= i; ++j){ b[i] = (b[i] + 1l ...

  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. Filestream读取或写入文件

    using System.IO;//引用 System.IO namespace filestream { public partial class Form1 : Form { public For ...

  2. ajax使用。

    <script> function createAjax(){ var request=false; //window对象中有XMLHttpRequest存在就是非IE,包括(IE7,IE ...

  3. 老外写的在桌面添加快捷方式(DELPHI XE5 ANDROID)

    UsesAndroidapi.JNI.GraphicsContentViewText, FMX.Helpers.Android,Androidapi.JNI.JavaTypes, FMX.Platfo ...

  4. spark 集合交集差集运算

    intersect except是spark提供的集合差集运算, 但是要求参与运算的两个dataframe,有相同的data Schema. 如果我想从 集合1(attribute1, attribu ...

  5. SRF之数据验证

    实现表单输入数据的验证,包括客户端验证和服务器端验证 如何使用 数据验证在业务层的实体类字段上增加数据验证的特性,例如 public class User { [Required(ErrorMessa ...

  6. hdu 1029

    #include"stdio.h" int main(void) { int n,x,y,t,i; while(scanf("%d",&n)!=EOF) ...

  7. 算法系列9《MD5》

    MD5即Message-Digest Algorithm 5(信息-摘要算法5),用于确保信息传输完整一致.是计算机广泛使用的杂凑算法之一(又译摘要算法.哈希算法),主流编程语言普遍已有MD5实现. ...

  8. oracle 几个时间函数探究

    近来经常用到时间函数,在此写一个笔记,记录自己的所得,希望也对您有所帮助. 1.对于一个时间如 sysdate:2015/1/30 14:16:03如何只得到年月日,同时它的数据类型不变化呢? 最容易 ...

  9. hdu 1880 魔咒词典

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1880 魔咒词典 Description 哈利波特在魔法学校的必修课之一就是学习魔咒.据说魔法世界有10 ...

  10. ios中的XMPP简介

      1.XMPP的定义 •XMPP:The Extensible Messaging and Presence Protocol(可扩展通讯和表示协议)   •XMPP可用于服务类实时通讯.表示和需求 ...