LIS问题算是DP中很基础很简单的一类问题,其经典算法时间复杂度为O(n^2);利用它本身的特点,结合二分法,即可设计出时间复杂度为O(n*log n)的优化算法。LIS的变形好像不太多,下面是两个很相似的双向LIS问题,其实第二个题的代码是直接用第一个题的代码改的。
Problem D
Wavio Sequence
Input: Standard Input
Output: Standard Output
Time Limit: 2 Seconds
Wavio is a sequence of integers. It has some interesting properties.
· Wavio is of odd length i.e. L = 2*n + 1.
· The first (n+1) integers of Wavio sequence makes a strictly increasing sequence.
· The last (n+1) integers of Wavio sequence makes a strictly decreasing sequence.
· No two adjacent integers are same in a Wavio sequence.
For example 1, 2, 3, 4, 5, 4, 3, 2, 0 is an Wavio sequence of length 9. But 1, 2, 3, 4, 5, 4, 3, 2, 2 is not a valid wavio sequence. In this problem, you will be given a sequence of integers. You have to find out the length of the longest Wavio sequence which is a subsequence of the given sequence. Consider, the given sequence as :
1 2 3 2 1 2 3 4 3 2 1 5 4 1 2 3 2 2 1.
Here the longest Wavio sequence is : 1 2 3 4 5 4 3 2 1. So, the output will be 9.
Input
The input file contains less than 75 test cases. The description of each test case is given below: Input is terminated by end of file.
Each set starts with a postive integer, N(1<=N<=10000). In next few lines there will be N integers.
Output
For each set of input print the length of longest wavio sequence in a line.
Sample Input Output for Sample Input
10 1 2 3 4 5 4 3 2 1 10 19 1 2 3 2 1 2 3 4 3 2 1 5 4 1 2 3 2 2 1 5 1 2 3 4 5 | 9 9 1
|
#include <cstdio>
#include <algorithm>
using namespace std;
int LIS[10010];
int LDS[10010];
int minlis[10010];
int data[10010];
int binary(int l, int r , int x){
int mid;
while(l <= r){
mid = (l + r) >> 1;
if(minlis[mid] < x) l = mid + 1;
else r = mid -1;
}
return l;
}
int main(){
int n, maxn, len;
while(~scanf("%d", &n)){
for(int i = 1; i <= n; i++) scanf("%d", data + i);
minlis[1] = data[1]; LIS[1] = 1; len = 1;
for(int i = 2; i <= n; i++){
LIS[i] = binary(1, len, data[i]);
minlis[LIS[i]] = data[i];
if(LIS[i] > len) len++;
}
minlis[1] = data[n]; LDS[n] = 1; len = 1;
for(int i = n-1; i > 0; i--){
LDS[i] = binary(1, len, data[i]);
minlis[LDS[i]] = data[i];
if(LDS[i] > len) len++;
}
maxn = 0;
for(int i = 1; i <= n; i++)
maxn = max(maxn , 2 * min(LIS[i] , LDS[i]) - 1);
printf("%d\n", maxn);
}
return 0;
}
How many elements you must throw out?
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 383 Accepted Submission(s): 152
You are given a sequence of numbers. Output the minimal number of elements you must throw out from the given sequence such that the remaining subsequence satisfies the condition described above.
6 1 4 6 5 2 1 5 2 2 2 2 2 0
0 4
#include <cstdio>
#define max(a , b) ((a) > (b) ? (a) : (b))
int LIS[55];
int LDS[55];
long long minlis[55];
long long data[55];
int binary(int l, int r , int x){
int mid;
while(l <= r){
mid = (l + r) >> 1;
if(minlis[mid] < x) l = mid + 1;
else r = mid -1;
}
return l;
}
int main(){
int n, maxn, len;
while(~scanf("%d", &n) && n != 0){
for(int i = 1; i <= n; i++) scanf("%lld", data + i);
minlis[1] = data[1]; LIS[1] = 1; len = 1;
for(int i = 2; i <= n; i++){
LIS[i] = binary(1, len, data[i]);
minlis[LIS[i]] = data[i];
if(LIS[i] > len) len++;
}
minlis[1] = data[n]; LDS[n] = 1; len = 1;
for(int i = n-1; i > 0; i--){
LDS[i] = binary(1, len, data[i]);
minlis[LDS[i]] = data[i];
if(LDS[i] > len) len++;
}
maxn = 0;
for(int i = 1; i <= n; i++)
maxn = max(maxn , LIS[i] + LDS[i] - 1);
printf("%d\n", n - maxn);
}
return 0;
}