| 20702 Maximum Product |
| Difficulty level: B; run time limit: 1000ms; operating space limit: 51200KB; code length limit: 2000000B |
| Question Description |
To enter a sequence of n elements, you need to find a continuous sub-sequence with the largest product, and output the product of the sequence, and if the maximum product is not a positive number, it should be output-1 (representing no solution). |
| Input |
| A total of two rows, the first row consists of a positive integer n, the number of elements representing the sequence s, the n elements of the second behavior sequence s, and 22 separated by a space. |
| Output |
| Results of the topic requirements |
| Input example |
3 2 4-3 |
| Output example |
| 8 |
| Other Notes |
| Data range: 1≤n≤18,-10≤s≤10;. Tip: The result range is within a long long range. |
Puzzle: Nest Meng treats it as O (N) for some value: Setting Mx[i],mi[i] represents the maximum minimum product of successive sub-sequences ending with I.
Nest Meng's algorithm can obviously be maintained online: read a number x, you can hit or start from the X, if the combo can be divided into two positive or negative situation (if lazy can write to a piece ...) )
WA a pitch: Nest Meng stared earnestly at this expression, carefully staring at the formula, seriously staring at this formula: if (negative) then Mx=max (x,mi*x), Mi=min (x,mi*x), (added scrolling), found to be wrong, because MX is variable = =. It's kind of a wake-up call: If you take the scroll of an array for granted, be sure to pay attention to which amount is changed = = ... At the same time tell Nest Meng, why not use two array of scrolling??!! Big dead??!! = =
Oh yes also: Write function remember to change the range oh ...
1#include <iostream>2#include <cstdio>3#include <cmath>4#include <algorithm>5#include <queue>6#include <cstring>7 #definePAU Putchar (")8 #defineENT Putchar (' \ n ')9 using namespacestd;Ten Const intmaxn= -+5; OneInlineintRead () { A Long Longx=0, sig=1;CharCh=GetChar (); - while(!isdigit (CH)) {if(ch=='-') sig=-1; ch=GetChar ();} - while(IsDigit (CH)) x=Ten*x+ch-'0', ch=GetChar (); the returnx*=Sig; - } -InlinevoidWriteLong Longx) { - if(x==0) {Putchar ('0');return;}if(x<0) Putchar ('-'), x=-x; + intlen=0;Long Longbuf[ -]; while(x) buf[len++]=x%Ten, x/=Ten; - for(inti=len-1; i>=0; i--) Putchar (buf[i]+'0');return; + } A intNLong Longmi,mx; at voidinit () { -N=read ();Long Longx,ans,t; -X=read (); mi=mx=ans=x; - for(intI=2; i<=n;i++){ -x=read (); - if(x>=0) Mx=max (x,mx*x), Mi=min (x,mi*x); in ElseT=mx,mx=max (x,mi*x), Mi=min (x,t*x);//hehe Oh -ans=Max (ANS,MX); to } + if(ans<=0) Write (-1);Elsewrite (ans); - return; the } * voidWork () { $ return;Panax Notoginseng } - voidprint () { the return; + } A intMain () {init (); work ();p rint ();return 0;}
COJ 0047 20702 Maximum product