POJ 3017 Cut The Sequence (monotonic queue optimization)

Source: Internet
Author: User

Title Link: http://poj.org/problem?id=3017

Test instructions: give you a sequence of length n, which requires dividing the sequence into arbitrary blocks, elements of each block, and less than M, so that the maximum and minimum values of all blocks

Analysis: This problem can soon be thought of a DP equation f[i]=min{f[j] +max{a[K]}} (b[i]<j<i,j<k<=i) b[i] to I and greater than M

The complexity of this equation is O (n^2), obviously to time out (how discuss say the data is weak = =)

Then is optimized, first of course is to optimize a maximum value of the queue, so that the queue of the first element of the team to the current position and not more than M,

Such a feasible solution is that f[i]=f[b[i]-1]+a[q[l]] (that is, the value of the first element of the team),

This is not the optimal solution, so we have to find the optimal solution in the queue, and a possible optimal solution can only be

f[q[j]]+ a[q[j +1]], that is, a[J] to be greater than the following number,

Obviously, if a[J] is smaller than the subsequent number, then we can divide a[J] into the back and get a better solution

Here the problem of finding the optimal solution is involved;

AC Code:

1#include <iostream>2#include <cstdio>3#include <cstdlib>4#include <cstring>5 using namespacestd;6typedefLong LongLL;7 Const intN =100020;8 LL Num[n];9 LL Sum[n];Ten LL F[n]; One intQ[n]; A intMain () { -     intN, I, J, St, Ed, p; - LL m; the      while(~SCANF ("%d%i64d", &n, &m)) { -Memset (F,-1,sizeof(f)); -St =0, ed =-1; -p =1; +sum[0] =0; -f[0] =0; +          for(i =1; I <= N; ++i) { Ascanf"%i64d", Num +i); atSum[i] = sum[i-1] + num[i];//the and of the first n items of statistics -             if(St >ed) { -St =0, ed =-1; -Q[++ed] =i; -             } -              while(St <= Ed && num[i] >= num[q[ed])--ed;//monotonic Queue Optimization inQ[++ed] =i; -              while(Sum[i]-sum[p-1] > m) + +p; to              while(St <= Ed && p > q[st]) st++;//the number of saved in the monotone queue has been deleted, then the bottom + +; +             if(St > ed)Continue;//The current queue is empty and returns directly -             if(f[p-1] != -1)//if the current P-bit, there are several; theF[i] = f[p-1] +Num[q[st]]; *              for(j = st +1; J <= Ed; ++j) { $                 if(f[q[j]-1] != -1)Panax NotoginsengF[i] = min (F[i], f[q[j-1]] +Num[q[j]]); -             } the         } +printf"%i64d\n", F[n]); A     } the     return 0; +}

POJ 3017 Cut The Sequence (monotonic queue optimization)

Related Article

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.