TYVJ 1305--length not exceeding m maximum continuous and "prefix and + monotone queue"

Source: Internet
Author: User

Title: http://www.tyvj.cn/p/1305

Defined:

Sum[i]=a[1]+a[2]+...+a[i] That is, sum[i] is the prefix of sequence A and

Dp[i] = sum[i]-min (sum[j]) (i-j<m) that is, dp[i] for the maximum continuous and the length of the end of I , which is less than M

The

The answer is Max (Dp[i]) (1≤i≤n)

#include <cstdio>#include<iostream>using namespacestd;intsum[300005];intst[300005];intMain () {intN, M, X, L, R, Minn, ans;  while(SCANF ("%d%d", &n, &m)! =EOF) {                 for(intI=1; i<=n; i++) {scanf ("%d", &x); Sum[i]= sum[i-1] +x; }
     //Maintain a monotonically incrementing queue L=r=0; St[r++]=0; Ans= sum[1]; for(intI=1; i<=n; i++) { while(L<r && i-st[l] > m) l++; Minn=Sum[st[l]]; if(Ans < sum[i]-minn) ans = sum[i]-Minn; while(L<r && Sum[i] < sum[st[r-1]]) r--; St[r++] =i; } printf ("%d\n", ans); } return 0;}

TYVJ 1305--length not exceeding m maximum continuous and "prefix and + monotone queue"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.