Title Description Description
This month's PKU month race a Chen did not attend, because at that time the school in the exam [a certain Chen often skipped classes, but a certain Chen is not strong enough to test also can escape the extent]. Besides, for Peking University race, the Buffalo usually have no hope to test well [in fact, some of the best results are a one-off].
Some Chen depressed. Then he will be immersed in the endless brush problem, every day to face a variety of colors of the status--wa,tle,re, and even mle,ce,pe what, he looked forward to the blue AC.
It is said that one of the pku of the RP has got the title and input data of a topic for a long time later, as shown below.
The input data consists of n test points, each of which has a maximum available score of M. The contestant needs to select any 3 integers, i,j,m0,1<=i<=j<=n, the range of tests selected by the contestants is from the first I to the first J, The desired score for each test point is M0. This is titled Special Judge[Special Evaluation], the system will traverse the marking from I to J Test point, for each test point that is traversed, if the current test point M is less than M0, then terminate the evaluation and judge the player to be divided into 0, Otherwise the system will be divided into 0 for the player score plus the m0[system initialization. If the final player has the same score as the maximum possible score, the player will ac the problem.
One Chen has already inscribed a long code for Ben, and now he needs to know the maximum possible score to verify that his output is the best solution. Please calculate the maximum score of the player and give a chance to make a watch.
[One chen: God. Please give me strength. I need a gorgeous ac ...]
Enter a description input Description
Input file first behavior an integer n, as in the title description. 0<n<=10^6
The next line consists of n integer m, as in the title description. 0<=m<2^31
outputs description output Description
The output file contains an integer that is the maximum score in the title description.
Force1: The Brute Force enumeration method, O (n^2), but apparently not. 2: Monotone stack: First of all, a monotone stack
A monotonic stack is similar to a monotone queue. First, the stack is a last-in, first-out, monotonic refers to the strict increment or decrement.
The monotonic stack has the following two properties:
1, if the monotonically increment stack, then the element from the top of the stack to the bottom of the stack is strictly increment. If the stack is monotonically decreasing, the elements from the top of the stack to the bottom of the stack are strictly decremented.
2, the closer the stack to the top of the element the more backward stack.
The difference between a monotone stack and a monotone queue is that the stack can only operate on top of the stack, so it is generally not limited in the place where the monotonic stack is applied, otherwise it can cause elements to fail to stack.
Elements into the stack process: for the monotonically incrementing the stack, if the current stack element is E, from the top of the stack to traverse the element, the element is less than e or equal to e pop-up stack, directly encountered an element greater than E or stack is empty, and then put E into the stack. For a monotonically decreasing stack, each popup is a element greater than e or equal to E.
An example of a monotonically increasing stack:
The stack elements are 3,4,2,6,4,5,2,3, respectively.
3 Stacks: (3)
3 out of Stack, 4 stacks: (4)
2 stack: (4,2)
2 Stacks, 4 stacks, 6 stacks: (6)
4 stack: (6,4)
4 out of Stack, 5 stacks: (6,5)
2 stack: (6,5,2)
2 out of Stack, 3 stacks: (6,5,3)
The left end is the bottom of the stack and the right side is the top. (Excerpt from http://blog.csdn.net/alongela/article/details/8227707)
Then the problem can be done with a monotonous stack.
And then for the top element of the stack, we go directly into the stack.
For smaller than the top of the stack, we put the element smaller than its stack, and the Maximum value update ((the current element subscript-out of the stack element into the stack time) * Out of the stack element), the last into the stack, the stack time is set to the last element out of the stack time (self-thinking why-).
O (n) over and over.
1#include <cstdio>2#include <cmath>3#include <cstring>4#include <algorithm>5 6 using namespacestd;7 8 intstack[1000001],top=0, time[1000001];9 Ten intMain () One { A intn,x; - Long Longmx=0; -scanf"%d",&n); the for(intI=1; i<=n+1; i++) - { - intD=i; - if(i<=n) scanf ("%d",&x); + Elsex=-1; - if(!top) stack[++top]=x,time[top]=1; + while(x<stack[top]&&top!=0) A { at Long LongSb= (Long Long) stack[top]* (I-time[top]); -mx=Max (MX,SB); -D=Time[top]; -top--; - } - if(X>stack[top]) stack[++top]=x,time[top]=D; in } -printf"%lld", MX); to return 0; +}
View Code
Title Description Description
Find the largest of all 0 sub-matrices in a 0,1 square, the so-called maximum is the number of O most.
Enter a description input Description
Input file first behavior integer n, where 1<=n<=2000, is the size of the square, followed by n lines each row has n 0 or 1, adjacent to the two numbers strictly separated by a space.
outputs description output Description
The output file contains only one line with an integer representing the maximum number of zeros in the total 0 sub-matrices required.
Force1: Enumerate the top left and right two points of each matrix, determine whether the elements inside are all 0, complexity 0 (N6), you can add an optimal pruning to accelerate.
2. First we use F[I][J] to indicate that there are several 0 elements above section J of row I.
Then we find that this is not the first question in each column.
All right.. That's it, remember when G[i][j] is 1 is f[i][j]=0,
F[I][J] can be optimized into 1-dimensional!
1#include <cstdio>2#include <cstring>3#include <cmath>4#include <algorithm>5 6 using namespacestd;7 8 intnum[2001],stack[2001],time[2001],top=0, ans=0, N;9 Ten BOOLg[2001]; One A voidWork () - { -top=0; thememset (Stack,0,sizeof(stack)); -memset (Time,0,sizeof(time)); - for(intI=1; i<=n+1; i++) - { + intd=i,x=Num[i]; - if(i==n+1) x=-1; + if(!top) stack[++top]=x,time[1]=1; A while(Stack[top]>x && top!=0 ) at { - intsb=stack[top]* (I-time[top]); -ans=Max (ANS,SB); -D=Time[top]; -top--; - } in if(stack[top]<x| | x==0) stack[++top]=x,time[top]=D; - } to } + - intMain () the { * intx; $scanf"%d",&n);Panax Notoginseng for(intI=1; i<=n;i++) - { the for(intj=1; j<=n;j++) + { Ascanf"%d",&x); theg[j]=x; + if(!g[j]) num[j]++; - Elsenum[j]=0; $ } $ Work (); - } -printf"%d", ans); the return 0; -}
View Code The third question is my hand.
Talk about the general test instructions. That is to give you a matrix, there are various numbers, to satisfy the top left and right of each matrix is less than equal to the top left and right and its sub-matrix also satisfies the condition of the maximum area of the matrix, the minimum length of the matrix, the width of 2. Force1 and optimization will not say. Positive solution: The 2*2 matrix is shrunk to a small matrix. Satisfies the condition is 0, does not satisfy is 1. (according to personal preferences) and then is the second problem of love solution. Remember the answer is (the current element subscript-the stack element in the stack time +1) * (out of the stack element +1): The code is also deleted. Let's get the brain mended.
"dp/monotone stacks" on some topics of monotonic stacks (Codevs 1159,codevs 2673)