Vijos 1243 production products [monotone queue optimization DP]

Source: Internet
Author: User

All right... As I dropped the first monotone queue optimization dp .... On the OJ of high school students .... And I have a 1.5-hour sample .... And then it's amazing 1A = = ...

This problem is more decisive than 8 of the 1005 difficult ah ... min inside things so wonderful ... K and P and J ... At first I thought that as long as a queue, got a half a day to find should be n queue ... = = ... It's fantastic to write ...

Transfer equations, see the code .... To break up ....

Summary below:

D[i] = min (f[k]) transfer equation (k with I do not drop, f is the only constant can be determined according to K in constant time). You can optimize the O (n) time for min by optimizing to O (1) using the Monotone queue optimization.

The specific operation process is generally:

Set K=[low, high].

1/throw F[high] into the DEQ. (Maintenance Deq, if the DEQ is not empty, first from the tail pop off non-monotonic elements)

2/Find elements from the first as Min (F[k]). (the element should satisfy the range of k, that is, the number is greater than low, not satisfied with the pop off).


Code:

#include <cstdio> #include <cstring> #include <iostream> #include <cmath> #include <string
> #include <vector> #include <map> #include <algorithm> using namespace std;  inline int Rint () {int x; scanf ("%d", &x), return x,} inline int max (int x, int y) {return (x>y), x:y;} inline int min (int x, int y) {return (x<y) x:y;} #define for (I, A, b) for (int i= (a); i<= (b); i++) #define FORD (I,A,B)
for (int i= (a); i>= (b), i--) #define REP (x) for (int i=0; i< (x); i++) typedef long long Int64;
#define INF (1<<30) const double EPS = 1e-8; #define BUG (s) cout<< #s << "=" <<s<< "" #define MAXN 7 #define MAXM 100002 #define MAXL 50002 int D [MAXM]	[MAXN];
D[I][J] Indicates the minimum time required for the first step to be completed by the J person.
Transfer equation: d[i][j] = min{D[k][p]+cost[j][i]-cost[j][k] +k | k=[i-l, I-1], k>=1, P=[1, N], p!=j}. 

Monotonic queue optimization: d[i][j] = min{D[k][p]-cost[j][k] | k=[i-l, I-1], k>=1, P=[1, N], p!=j} +cost[j][i] + K.//D[0][p] =-K; INT CoST[MAXN][MAXM];

Cost[i][j], represents the first person, the time required to complete the 1~j step and.
int DEQ[MAXM][MAXN];
int FRONT[MAXN], TAIL[MAXN];	int F[MAXM][MAXN];
	MIN (f) int main () {memset (d, 0, sizeof (d));
	memset (cost, 0, sizeof);
	int M=rint (), N=rint (), K=rint (), L=rint ();
			For (i, 1, n) {for (J, 1, m) {int t = Rint ();	Cost[i][j]=cost[i][j-1]+t;
	Prefix and}} for (I, 1, n) {d[0][i] =-K;	}//front = tail = 1;
	Subscript starting from 1 for (I, 1, n) {front[i] = tail[i] = 1; } for (I, 1, m)//D[i] = min (d[k][p]-cost[k]) + cost[i] + K {for (J, 1, N) {//f[i-1] thrown into DEQ f[i-1][j] =
			INF;
				For (p, 1, N)//min (D[k]-cost[k]) {if (p = = j) Continue;
				F[i] = min (F[i], d[k][p]-cost[j][k]);		
				F[i] = min (F[i], d[i][p]-cost[j][i]);		F[i-1][j] = min (F[i-1][j], d[i-1][p]-cost[j][i-1]);
			Because of k<=i-1, so here with I-1, d[i][p] not all come out}//bug (i-1); bug (j); Bug (F[i-1][j]) <<endl;
			Bug (F[deq[tail[j]-1][j]][j]) <<endl; while (Front[j]<tail[j] && f[i-1][j]&LT;F[DEQ[TAIL[J]-1][J]][J]) tail[j]--;		DEQ[TAIL[J]++][J] = i-1;

			This is i-1 is not I ....!!!! ..... by Deq min (F[k]), K=[i-l, i-1]//int low = i-l<1?
			1:i-l; int low = i-l<0?			0:i-l;
			Since there is a boundary, the subscript expands to 0//int high = i-1;
			Bug (Deq[front[j]][j]) <<endl;
			while (Deq[front[j]][j]<low) front[j]++;
			Bug (deq[front[j]][j]) <<endl;
			int minx = f[deq[front[j]][j]][j];
			D[I][J] = Minx + cost[j][i] + K;
			Bug (Minx) <<endl;
		Bug (i); Bug (j); Bug (D[i][j]) <<endl;
	}} int minx = INF;
	For (i, 1, n) {minx = min (Minx, d[m][i]);
} printf ("%d\n", Minx); }

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.