Maximum Sub-sequence Product
Maximum Sub-sequence Product |
Bob needs money, and since he knows you are here, he decided to gambleintelligently. The game is rather simple: each player gets a sequence ofintegers. The players must determine, by using their mega-pocket computers,which is the maximum product value which can be computed with non emptysub-sequences of consecutive numbers from the given sequence. The winner isthe one which gets first the right result.
Can you help Bob with a program to compute quickly the wanted product,particularly when the sequence is quite long ?
Input
The input file contains sequences of numbers. Eachnumber will have at most 5 digits. There will be atmost 100 numbers in each sequence. Each sequence starts on a newline and may continue on several subsequent lines. Each sequence ends withthe number -999999 (which is not part of the sequence).Output
The maximum sub-sequence product for each sequence must be written on thestandard output, on a different line. A simple example is illustrated inthe sample below.Sample Input
1 2 3 -999999 -5 -2 2 -30 -999999 -8 -999999 -1 0 -2 -999999
Sample Output
6 120 -8 0
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
import java.util.ArrayList;
import java.math.BigInteger;
public class Main
{
public static final boolean DEBUG = false;
public StreamTokenizer tokenizer;
public BufferedReader cin;
public PrintWriter cout;
public ArrayList<Integer> v = new ArrayList<Integer>();
public void init()
{
try {
if (DEBUG) {
cin = new BufferedReader(new InputStreamReader(
new FileInputStream("d:\\OJ\\uva_in.txt")));
} else {
cin = new BufferedReader(new InputStreamReader(System.in));
}
cout = new PrintWriter(new OutputStreamWriter(System.out));
tokenizer = new StreamTokenizer(cin);
} catch (Exception e) {
e.printStackTrace();
}
}
public String next()
{
try {
tokenizer.nextToken();
if (tokenizer.ttype == StreamTokenizer.TT_EOF)
return null;
else if (tokenizer.ttype == StreamTokenizer.TT_NUMBER) {
return String.valueOf((int)tokenizer.nval);
} else if (tokenizer.ttype == StreamTokenizer.TT_WORD) {
return tokenizer.sval;
} else return null;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public boolean input()
{
v.clear();
while (true) {
String s = next();
if (s == null) return false;
if ("-999999".compareTo(s) == 0) break;
else {
v.add(Integer.parseInt(s));
}
}
return true;
}
public void solve()
{
BigInteger Min = BigInteger.valueOf(v.get(0)), Max = Min;
BigInteger ans = Max;
for (int i = 1, len = v.size(); i < len; i++) {
BigInteger M = BigInteger.valueOf(v.get(i));
if (M.compareTo(BigInteger.valueOf(v.get(i)).multiply(Max)) < 0) {
M = BigInteger.valueOf(v.get(i)).multiply(Max);
}
if (M.compareTo(BigInteger.valueOf(v.get(i)).multiply(Min)) < 0) {
M = BigInteger.valueOf(v.get(i)).multiply(Min);
}
BigInteger m = BigInteger.valueOf(v.get(i));
if (m.compareTo(BigInteger.valueOf(v.get(i)).multiply(Max)) > 0) {
m = BigInteger.valueOf(v.get(i)).multiply(Max);
}
if (m.compareTo(BigInteger.valueOf(v.get(i)).multiply(Min)) > 0) {
m = BigInteger.valueOf(v.get(i)).multiply(Min);
}
Max = M;
Min = m;
if (ans.compareTo(Max) < 0) {
ans = Max;
}
}
cout.println(ans.toString());
cout.flush();
}
public static void main(String[] args)
{
Main solver = new Main();
solver.init();
while (solver.input()) {
solver.solve();
}
}
}