Vijos P1243 Production products (monotone queue +DP)

Source: Internet
Author: User

P1243 Production ProductsDescribe

After a period of operation, Dd_engi's Oi shop is not satisfied with the purchase of products from other suppliers to put on shelves, but to begin to produce their own products! The production of the product requires a M-step, each of which can be done on any one of the N machines, but the production steps must be executed in strict order. Because of the different performance of the N machines, the time required to complete each step is different. Machine I completed the time of the J step is t[i,j]. It also takes a certain amount of time to move semi-finished products from one machine to another machine. At the same time, in order to ensure the safety and quality of the product, each machine can only be completed in a continuous L-step product. That is, if a machine has completed the L-step of the product in succession, the next step must be done with a different machine. Now, the first product of Dd_engi's Oi store is about to start production, so how long will it take?
One day azuki.7 to the movement said: This topic is too simple, we change the scope of the topic to change
This is a difficult question for rookie, and he wants you to help him solve it.

Format input Format

The first line has four integers m, N, K, L
The following n rows have m integers per line. The J integer of Line i+1 is t[j,i].

Output format

The output has only one row, indicating the shortest time required.

Example 1 sample input 1[copy]
3 2 0 22 2 31 3 1
Sample output 1[Copy]
4
Limit

1s

Tips

For 50% of data, n<=5,l<=4,m<=10000
For 100% of data, n<=5, l<=50000,m<=100000

Source

First "Oi Shop Cup" dd_engi original topic

Ideas

The monotone queue optimizes DP.

F[I][J] indicates that the first step is taken into account, and the minimum time at which I step is completed by J. With Transfer type:

F[i][j]=min{f[k][p]-sum[k][j]}+sum[i][j] , I-l<=k<=i-1

The parts in parentheses can be maintained with n monotone queues. A[][] is an auxiliary array,

A[i][j]=min{f[i][k]}-sum[i][j]+k,1<=k<=m

Represents F[I][J] The minimum value that can be taken in parentheses, and the minimum value of the monotonic queue J maintenance interval [i-l,i-1] a[][j].

Note the monotone queue notation.

PS: This paper is good for beginners: HTTP://PAN.BAIDU.COM/S/1GEXNMWJ

Code

1#include <cstdio>2#include <cstring>3#include <iostream>4 using namespacestd;5 6 Const intMAXN =200000+Ten;7 Const intINF = 1e9+1e9;8 9 intl[6],r[6],q[maxn][6];Ten intf[maxn][6], a[maxn][6], b[maxn][6]; One intN,m,k,lim; A  - voidPushintIintj) { -      while(L[j]<r[j] && a[i][j]<=a[q[r[j]-1][J]][J]) r[j]--; theq[r[j]++][j]=i; - } - intPopintIintj) { -      while(L[j]<r[j] && Q[l[j]][j]<i-lim) l[j]++; +     returnA[q[l[j]][j]][j]; - } + intReadint&x) { A     CharC=getchar (); while(!isdigit (c)) c=GetChar (); atx=0; while(IsDigit (c)) x=x*Ten+c-'0', c=GetChar (); - } - intMain () { - Read ( N), read (m), read (K), read (LIM); -      for(intI=1; i<=m;i++) -          for(intj=1; j<=n;j++) { in read (b[j][i]); -b[j][i]+=b[j-1][i]; to         } +      for(intj=1; j<=m;j++) Push (0, j); -      for(intI=1; i<=n;i++) the     { *          for(intj=1; j<=m;j++) $F[i][j]=pop (I,J) +B[i][j];Panax Notoginseng          for(intj=1; j<=m;j++) {//add a[i][to the monotone queue] -a[i][j]=INF; the              for(intk=1; k<=m;k++)if(k!=j) +a[i][j]=min (a[i][j],f[i][k]); Aa[i][j]=a[i][j]-b[i][j]+K; the push (I,J); +         } -     } $     intans=f[n][1]; $      for(intI=1; i<=m;i++) ans=min (ans,f[n][i]); -printf"%d\n", ans); -     return 0; the}

Vijos P1243 Production products (monotone queue +DP)

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.