uva10534 hdu2198 双向LIS问题

         LIS问题算是DP中很基础很简单的一类问题,其经典算法时间复杂度为O(n^2);利用它本身的特点,结合二分法,即可设计出时间复杂度为O(n*log n)的优化算法。LIS的变形好像不太多,下面是两个很相似的双向LIS问题,其实第二个题的代码是直接用第一个题的代码改的。


uva10543:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=17&page=show_problem&problem=1475


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


Problem Description
You have a sequence of numbers from which you must create the longest subsequence satisfying the following condition: it can be 'cut' into two parts that share exactly one common element (the last element of the first part is the first element of the second part), and the first part is sorted in strictly ascending order while the second part is sorted in strictly descending order. For example, the sequence { 1, 4, 6, 5, 2, 1 } can be 'cut' into { 1, 4, 6 } and { 6, 5, 2, 1 }. The two parts share the 6(see the following graph), and the first sequence is sorted in ascending order while the second sequence is sorted in descending order.

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.
 

Input
There are multiple test cases. At first line of each case, there's an integer N (1<=N<=50) and N integers followed at second line representing the subsequence, each of which ranges from 1 to 1000000000.Input ends when N is 0.
 

Output
Output the result for each case in one line.
 

Sample Input
  
  
6 1 4 6 5 2 1 5 2 2 2 2 2 0
 

Sample Output
  
  
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;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值