Good news OR bad news

topic

1. We can first consider the violent approach, first break the ring into a column, and then judge one by one to find the prefix sum, and then judge one by one whether all are >=0, and the complexity is O(n^2)
2. The monotonic queue maintains the least The prefix sum, for each k+n-1, we use a monotonic queue to maintain the minimum value of k–k+n-1, and subtract s[k-1] to determine whether it is legal. If the smallest is already legal, then it must be legal

#include<bits/stdc++.h>
#define maxn 2000010
using namespace std;
int n,m;
long long a[maxn],q[maxn],sum[maxn];
int head=1,tail;
inline long long read()
{
    
    
    long long res=0,f=1;char ch=getchar();
    while(!isdigit(ch)){
    
    if(ch=='-')f=-f;ch=getchar();}
    while(isdigit(ch)){
    
    res=(res<<1)+(res<<3)+(ch&15);ch=getchar();}
    return res*f;
}
int ans;
int main()
{
    
    
    cin>>n;
    for(int i=1;i<=n;i++)a[i]=read(),a[i+n]=a[i];
	for(int i=1;i<n*2;i++)sum[i]+=sum[i-1]+a[i];
    for(int i=1;i<n*2;i++)
    {
    
       
        if(q[head]<=i-n)head++;
//        cout<<q[head]<<' ';
        while(head<=tail&&(sum[i]<=sum[q[tail]]))tail--;
        q[++tail]=i;
        if(i>=n&&sum[q[head]]-sum[i-n]>=0)ans++;
    }
    cout<<ans;
    return 0;
}

Guess you like

Origin blog.csdn.net/yhhy666/article/details/109266770