hdu 4193 递增单调队列

双向队列STL:

https://blog.csdn.net/z_xindong/article/details/81296117

题目链接:

http://acm.hdu.edu.cn/showproblem.php?pid=4193

Non-negative Partial Sums

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3722    Accepted Submission(s): 1215


 

Problem Description

You are given a sequence of n numbers a0,..., an-1. A cyclic shift by k positions (0<=k<=n-1) results in the following sequence: ak ak+1,..., an-1, a0, a1,..., ak-1. How many of the n cyclic shifts satisfy the condition that the sum of the first i numbers is greater than or equal to zero for all i with 1<=i<=n?

 

 

Input

Each test case consists of two lines. The first contains the number n (1<=n<=106), the number of integers in the sequence. The second contains n integers a0,..., an-1(-1000<=ai<=1000) representing the sequence of numbers. The input will finish with a line containing 0.

 

 

Output

For each test case, print one line with the number of cyclic shifts of the given sequence which satisfy the condition stated above.

 

 

Sample Input

 

3 2 2 1 3 -1 1 1 1 -1 0

 

 

Sample Output

 

3 2 0

 

 

Source

SWERC 2011

 

 

Recommend

lcy

题目大意:

对于以i位置开头的队列(i前面的元素fang放到后面,类似形成环)能否所有的前缀和都是大于等于0的,

问:能形成多少这样的队列

思路:

单调队列寻找从i开始到i+n中的最小前缀和,然后减去i-1的前缀和就好了

sum[q[head]]-sum[i-n];(因为必须先寻找前n个的最小值,从n+1正式开始判断)

This is the code

#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<iostream>
#include<iomanip>
#include<list>
#include<map>
#include<queue>
#include<sstream>
#include<stack>
#include<string>
#include<set>
#include<vector>
using namespace std;
#define PI acos(-1.0)
#define pppp cout<<endl;
#define EPS 1e-8
#define LL long long
#define ULL unsigned long long     //1844674407370955161
#define INT_INF 0x3f3f3f3f      //1061109567
#define LL_INF 0x3f3f3f3f3f3f3f3f //4557430888798830399
// ios::sync_with_stdio(false);
// 那么cin, 就不能跟C的 scanf,sscanf, getchar, fgets之类的一起使用了。
const int dr[]={0, 0, -1, 1, -1, -1, 1, 1};
const int dc[]={-1, 1, 0, 0, -1, 1, -1, 1};
inline int read()//输入外挂
{
    int ret=0, flag=0;
    char ch;
    if((ch=getchar())=='-')
        flag=1;
    else if(ch>='0'&&ch<='9')
        ret = ch - '0';
    while((ch=getchar())>='0'&&ch<='9')
        ret=ret*10+(ch-'0');
    return flag ? -ret : ret;
}
const int MAXN=1e6+5;
int a[2*MAXN];
int sum[2*MAXN];
int q[2*MAXN];//单调队列
int main()
{
    int n;
    while(scanf("%d",&n))
    {
        if(!n)
            break;
        for(int i=1;i<=n;++i)
        {
            scanf("%d",&a[i]);
            a[i+n]=a[i];
        }
        sum[0]=0;
        for(int i=1;i<=n*2;++i)
            sum[i]=sum[i-1]+a[i];
        int head=0;
        int tail=0;
        int cnt=0;
        for(int i=1;i<n*2;++i)
        {
            while(head<tail&&sum[i]<=sum[q[tail-1]])//单调队列,递增
                tail--;
            q[tail++]=i;
            if(i>=n&&sum[q[head]]-sum[i-n]>=0)//判断当前是否符合题意
                cnt++;
            while(head<tail&&i-q[head]>=n-1)//表示不能让队列长度超过n
                head++;
        }
        printf("%d\n",cnt);
    }
    return 0;
}

This is the code//双向队列版

#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<iostream>
#include<iomanip>
#include<list>
#include<map>
#include<queue>
#include<sstream>
#include<stack>
#include<string>
#include<set>
#include<vector>
using namespace std;
#define PI acos(-1.0)
#define pppp cout<<endl;
#define EPS 1e-8
#define LL long long
#define ULL unsigned long long     //1844674407370955161
#define INT_INF 0x3f3f3f3f      //1061109567
#define LL_INF 0x3f3f3f3f3f3f3f3f //4557430888798830399
// ios::sync_with_stdio(false);
// 那么cin, 就不能跟C的 scanf,sscanf, getchar, fgets之类的一起使用了。
const int dr[]={0, 0, -1, 1, -1, -1, 1, 1};
const int dc[]={-1, 1, 0, 0, -1, 1, -1, 1};
inline int read()//输入外挂
{
    int ret=0, flag=0;
    char ch;
    if((ch=getchar())=='-')
        flag=1;
    else if(ch>='0'&&ch<='9')
        ret = ch - '0';
    while((ch=getchar())>='0'&&ch<='9')
        ret=ret*10+(ch-'0');
    return flag ? -ret : ret;
}
const int MAXN=1e6+5;
int a[2*MAXN];
int sum[2*MAXN];
//int q[2*MAXN];//单调队列
int main()
{
    int n;
    while(scanf("%d",&n))
    {
        if(!n)
            break;
        for(int i=1;i<=n;++i)
        {
            scanf("%d",&a[i]);
            a[i+n]=a[i];
        }
        sum[0]=0;
        for(int i=1;i<=n*2;++i)
            sum[i]=sum[i-1]+a[i];
        deque<int > q;
        q.clear();
        int cnt=0;
        for(int i=1;i<n*2;++i)
        {
            while(!q.empty()&&sum[i]<=sum[q.back()])
                q.pop_back();
            q.push_back(i);
            if(i>=n&&sum[q.front()]-sum[i-n]>=0)//判断当前是否符合题意
                cnt++;
            while(!q.empty()&i-q.front()>=n-1)//表示不能让队列长度超过n
                q.pop_front();
        }
        printf("%d\n",cnt);
    }
    return 0;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值