uva 10534 - Wavio Sequence Solution
uva 10534 - Wavio Sequence Solution
#include<algorithm>
#include<iostream>
#include<vector>
using namespace std;
void binary_search(int *arr, int search_value, int start, int end, int &index){
if (start == end){
index = start;
return;
}
int mid = (start + end) / 2;
if (arr[mid] == search_value){
index = mid;
return;
}
else if (arr[mid + 1] == search_value){
index = mid + 1;
return;
}
else if (arr[mid]<search_value && arr[mid + 1]>search_value){
index = mid + 1;
return;
}
else if (arr[mid] > search_value){
binary_search(arr, search_value, start, mid, index);
}
else{
binary_search(arr, search_value, mid + 1, end, index);
}
}
void calculate_lis(vector<int>&arr, int n, vector<int>&result){
int *temp, i, max = 0, j, index;
temp = new int[n];
temp[0] = 0;
result.push_back(1);
for (i = 1; i < n; i++){
if (arr[temp[max]] < arr[i]){
max++;
temp[max] = i;
result.push_back(max+1);
}
else if (arr[temp[max]] == arr[i]){
result.push_back(result[temp[max]]);
}
else if (arr[temp[0]] == arr[i]){
result.push_back(result[temp[0]]);
}
else if (arr[temp[0]] > arr[i]){
temp[0] = i;
result.push_back(1);
}
else{
int *temp_arr = new int[max];
for (j = 1; j <= max; j++)
temp_arr[j - 1] = arr[temp[j]];
binary_search(temp_arr, arr[i], 0, max - 1, index);
index++;
if (arr[index] == arr[i]){
result.push_back(result[temp[index]]);
}
else{
result.push_back(result[temp[index]]);
temp[index] = i;
}
}
}
}
int main() {
vector<int> vec1, vec2, res1, res2;
int i, n, ans, x;
while (cin >> n){
if (n == 1){
cin >> x;
cout << "1" << endl;
continue;
}
vec1.clear();
res1.clear();
vec2.clear();
res2.clear();
for (i = 0; i < n; i++){
cin >> x;
vec1.push_back(x);
vec2.push_back(x);
}
reverse(vec2.begin(), vec2.end());
calculate_lis(vec1, n, res1);
calculate_lis(vec2, n, res2);
reverse(res2.begin(),res2.end());
ans = 0;
for (i = 0; i < n; i++){
ans = max(ans,min(res1[i],res2[i]));
}
cout << ans * 2 - 1 << endl;
}
return 0;
}
Comments
Post a Comment