tyvj1305 (maximum subsequence sum ------ monotonic queue)

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

const  int maxn = 1000000 ;

int q[maxn],a[maxn];

int main(){
    int n;
    int m;
    scanf("%d%d",&n,&m);
    for (int i=1;i<=n;i++){
        scanf("%d",&a[i]);
    }
    for (int i=1;i<=n;i++){
        a[i]+=a[i-1];
    }
    int l, r;
    l=r=1;
    q[l] = 0 ;
     int ans=-( 0x3f3f3f3f );
     for ( int i= 1 ;i<=n;i++ ){
         while (l<=r&&q[l]<im) l++; // guarantee the validity of the answer 
        Ans=max(ans,a[i]-a[q[l]]); // Because the monotonicity of the queue is guaranteed, the head element of the queue is the current optimal 
        while (l<=r&&a[q[r] ]>=a[i]) r--; // Ensure the monotonicity of the queue, that is, ensure the optimal answer to the head of the queue 
        q[++r]= i;
    }
    printf("%d\n",ans);
return 0;
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325093916&siteId=291194637