hdu3415 Max Sum of max-k-sub-sequence monotone queue

Source: Internet
Author: User

Tag: Hdu3415   max sum of max-k-sub    monotone queue    

hdu3415 Max Sum of max-k-sub-sequence//monotone queue//first thought of preprocessing prefix and using s[i]-s[j] to indicate (J,i] segment and//after the problem is converted to a minimum s[j], so that it can be monotonous queue/ /Find minimum value. The queue is maintained in the beginning of the interval J, we insert the queue is j-1, because//this time s[i]-s[j-1] just is [j,i] segment closed interval and//here in two ways, one is STL, one is manual simulation, the speed of both, testing// The results in Hangzhou Electric test are the same, 499ms. The path to the monotonous queue is still long. # include <cstdio> #include <queue> #include <algorithm> #include <iostream> #include <cstring>using namespace Std;typedef long long ll;const int MAXN = 1e5 + 8;ll a[maxn*2];ll sum[maxn*2];ll x    [MAXN * 2];int DEQ[MAXN * 2];int n,k;int mod;void input () {scanf ("%d%d", &n,&k);    for (int i=1;i<=n;i++) {scanf ("%lld", &a[i]);    } for (int i=n+1;i<=2*n;i++) {A[i] = a[i-n];    } Sum[0] = 0;    for (int i=1;i<=n;i++) {Sum[i] = Sum[i-1] + a[i];    } for (int i=1;i<k;i++) {sum[i+n]= sum[i+n-1] + a[i];    } mod = n; n = n + k-1;}    void Solve () {int s,e;    int head = 0,tail = 0;    ll mx = -1e10; for (int i=1;i<=n;i++) {while (TaIl > Head && sum[i-1]<=sum[deq[tail-1]]) tail--;        while (Tail > Head && deq[head]<i-k) head++;        deq[tail++] = i-1;            if (Sum[i]-sum[deq[head]] > mx) {mx = sum[i]-sum[deq[head]];            s = deq[head] + 1;        e = i;    }} if (E > MoD) e-= mod;    printf ("%lld%d%d\n", mx,s,e); printf ("%lld%d\n", X[mk], (mk+k)%n);} void Solve () {//int s,e;//deque<int> deq;//deq.clear ();/ll mx = -1e10;//for (int i=1;i<=n;i + +) {//while (!deq.empty () && sum[i-1]<=sum[deq.back ()])//Deq.pop_back (),//while (!DEQ . Empty () && Deq.front () <i-k)//Deq.pop_front ();//Deq.push_back (i-1);/if (Sum[i]-S Um[deq.front ()]>mx) {//mx = sum[i]-Sum[deq.front ()];//s = deq.front () + 1;//e = i; }//}////if (E > MoD)//e-= mod;//printf ("%llD%d%d\n ", mx,s,e);//////printf ("%lld%d\n ", X[mk], (mk+k)%n);//}int Main () {//freopen (" 1.txt "," R ", stdin);    int t;    scanf ("%d", &t);        while (t--) {input ();    Solve (); } return 0;}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

hdu3415 Max Sum of max-k-sub-sequence 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.