(UVA) Wavio Sequence - Solution
Link to the problem: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1475
The solution below uses a modified version of the Longest Increasing Sequence (LIS).
import java.io.*;
import java.util.*;
class Main {
public static int reader(BufferedReader br) throws NumberFormatException, IOException {
int n;
int resp = 0;
while (true) {
n = br.read();
if (n == -1) return -1;
if (n >= '0' && n <= '9') {
break;
}
}
while (true) {
resp = resp*10 + n-'0';
n = br.read();
if (n == -1) return -1;
if (n < '0' || n > '9') {
break;
}
}
return resp;
}
public void process() throws NumberFormatException, IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
while (true) {
int qty = reader(br);
if (qty == -1) break;
Number[] numbers = new Number[qty];
for (int i = 0; i < qty; i++) {
int n = reader(br);
numbers[i] = new Number(n, 1, 1);
}
TreeMap<Integer, Integer> mapIncreasingBottomUp = new TreeMap<>();
TreeMap<Integer, Integer> mapIncreasingTopDown = new TreeMap<>();
for (int i = 0, j = qty-1; i < qty && j >= 0; i++, j--) {
Map.Entry<Integer,Integer> entryIncreasingBottomUp = mapIncreasingBottomUp.ceilingEntry(numbers[i].number);
if (entryIncreasingBottomUp == null) { // there is not any key greater than or equal to the given key
numbers[i].increasingBottomUp = mapIncreasingBottomUp.size()+1;
mapIncreasingBottomUp.put(numbers[i].number, mapIncreasingBottomUp.size()+1);
}
else { // found the least key greater than or equal to the given key
numbers[i].increasingBottomUp = entryIncreasingBottomUp.getValue();
mapIncreasingBottomUp.remove(entryIncreasingBottomUp.getKey());
mapIncreasingBottomUp.put(numbers[i].number, entryIncreasingBottomUp.getValue());
}
Map.Entry<Integer,Integer> entryIncreasingTopDown = mapIncreasingTopDown.ceilingEntry(numbers[j].number);
if (entryIncreasingTopDown == null) { // there is not any key greater than or equal to the given key
numbers[j].increasingTopDown = mapIncreasingTopDown.size()+1;
mapIncreasingTopDown.put(numbers[j].number, mapIncreasingTopDown.size()+1);
}
else { // found the least key greater than or equal to the given key
numbers[j].increasingTopDown = entryIncreasingTopDown.getValue();
mapIncreasingTopDown.remove(entryIncreasingTopDown.getKey());
mapIncreasingTopDown.put(numbers[j].number, entryIncreasingTopDown.getValue());
}
}
int maxWavio = 1;
for (int i = 0; i < qty; i++) {
int minCommom = Math.min(numbers[i].increasingBottomUp, numbers[i].increasingTopDown);
int length = minCommom*2 - 1;
maxWavio = Math.max(maxWavio, length);
}
System.out.println(maxWavio);
}
return;
}
public static void main(String[] args) throws NumberFormatException, IOException {
Main m = new Main();
m.process();
System.exit(0);
}
}
class Number {
int number;
int increasingBottomUp;
int increasingTopDown;
public Number(int n, int iBU, int iTD) {
number = n;
increasingBottomUp = iBU;
increasingTopDown = iTD;
}
}
The solution below uses a modified version of the Longest Increasing Sequence (LIS).
import java.io.*;
import java.util.*;
class Main {
public static int reader(BufferedReader br) throws NumberFormatException, IOException {
int n;
int resp = 0;
while (true) {
n = br.read();
if (n == -1) return -1;
if (n >= '0' && n <= '9') {
break;
}
}
while (true) {
resp = resp*10 + n-'0';
n = br.read();
if (n == -1) return -1;
if (n < '0' || n > '9') {
break;
}
}
return resp;
}
public void process() throws NumberFormatException, IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
while (true) {
int qty = reader(br);
if (qty == -1) break;
Number[] numbers = new Number[qty];
for (int i = 0; i < qty; i++) {
int n = reader(br);
numbers[i] = new Number(n, 1, 1);
}
TreeMap<Integer, Integer> mapIncreasingBottomUp = new TreeMap<>();
TreeMap<Integer, Integer> mapIncreasingTopDown = new TreeMap<>();
for (int i = 0, j = qty-1; i < qty && j >= 0; i++, j--) {
Map.Entry<Integer,Integer> entryIncreasingBottomUp = mapIncreasingBottomUp.ceilingEntry(numbers[i].number);
if (entryIncreasingBottomUp == null) { // there is not any key greater than or equal to the given key
numbers[i].increasingBottomUp = mapIncreasingBottomUp.size()+1;
mapIncreasingBottomUp.put(numbers[i].number, mapIncreasingBottomUp.size()+1);
}
else { // found the least key greater than or equal to the given key
numbers[i].increasingBottomUp = entryIncreasingBottomUp.getValue();
mapIncreasingBottomUp.remove(entryIncreasingBottomUp.getKey());
mapIncreasingBottomUp.put(numbers[i].number, entryIncreasingBottomUp.getValue());
}
Map.Entry<Integer,Integer> entryIncreasingTopDown = mapIncreasingTopDown.ceilingEntry(numbers[j].number);
if (entryIncreasingTopDown == null) { // there is not any key greater than or equal to the given key
numbers[j].increasingTopDown = mapIncreasingTopDown.size()+1;
mapIncreasingTopDown.put(numbers[j].number, mapIncreasingTopDown.size()+1);
}
else { // found the least key greater than or equal to the given key
numbers[j].increasingTopDown = entryIncreasingTopDown.getValue();
mapIncreasingTopDown.remove(entryIncreasingTopDown.getKey());
mapIncreasingTopDown.put(numbers[j].number, entryIncreasingTopDown.getValue());
}
}
int maxWavio = 1;
for (int i = 0; i < qty; i++) {
int minCommom = Math.min(numbers[i].increasingBottomUp, numbers[i].increasingTopDown);
int length = minCommom*2 - 1;
maxWavio = Math.max(maxWavio, length);
}
System.out.println(maxWavio);
}
return;
}
public static void main(String[] args) throws NumberFormatException, IOException {
Main m = new Main();
m.process();
System.exit(0);
}
}
class Number {
int number;
int increasingBottomUp;
int increasingTopDown;
public Number(int n, int iBU, int iTD) {
number = n;
increasingBottomUp = iBU;
increasingTopDown = iTD;
}
}
Comments
Post a Comment