zoukankan      html  css  js  c++  java
  • HDU

    There is a sequence X (i.e. x[1], x[2], ..., x[n]). We define increasing subsequence of X 

    as x[i1], x[i2],...,x[ik], which satisfies follow conditions: 
    1) x[i1] < x[i2],...,<x[ik]; 
    2) 1<=i1 < i2,...,<ik<=n 

    As an excellent program designer, you must know how to find the maximum length of the 
    increasing sequense, which is defined as s. Now, the next question is how many increasing 
    subsequence with s-length can you find out from the sequence X. 

    For example, in one case, if s = 3, and you can find out 2 such subsequence A and B from X. 
    1) A = a1, a2, a3. B = b1, b2, b3. 
    2) Each ai or bj(i,j = 1,2,3) can only be chose once at most. 

    Now, the question is: 
    1) Find the maximum length of increasing subsequence of X(i.e. s). 
    2) Find the number of increasing subsequence with s-length under conditions described (i.e. num).

    Input

    The input file have many cases. Each case will give a integer number n.The next line will 

    have n numbers.OutputThe output have two line. The first line is s and second line is num.Sample Input
    4
    3 6 2 5
    Sample Output
    2
    2

    题解:

        int sum = 1;

        持续求LIS并把每次的数都删去,如果LIS长度不变则sum++,否则停止并输出sum。

    代码:

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    
    using namespace std;
    
    const int MAXN = 100005;
    
    int book[MAXN]; /*记录最小末尾数在board的下标*/ 
    bool mark[MAXN];/*标记该整数是否删除*/
    int board[MAXN];/*存储整数*/
    int pre[MAXN];/*记录每个整数的前接整数在board的下标*/
    int MaxLen;/*第一次的LIS*/
    
    int B_S(int a,int len){
    	int left = 0;
    	int right = len;
    	while(left<right){
    		int mid = left+(right-left)/2;
    		if(board[book[mid]]>=a)right = mid;
    		else left = mid+1;
    	}
    	return left;
    }
    
    int LIS(int length){
    	int len = 0;
    	memset(book,-1,sizeof(book));
    	memset(pre,-1,sizeof(pre));
    	for(int i=0 ; i<length ; i++){
    		if(mark[i]){
    			int temp = B_S(board[i],len);
    			if(book[temp] == -1)len++;
    			book[temp] = i;
    			if(temp)pre[i] = book[temp-1];
    		}
    	}
    	int t = book[len-1];
    	while(t != -1){
    		mark[t] = false;
    		t = pre[t];
    	}
    	return len;
    }
    
    
    int main(){
    	
    	int N;
    	while(scanf("%d",&N)!=EOF){
    		for(int i=0 ; i<N ; i++)scanf("%d",&board[i]);
    		memset(mark,true,sizeof(mark));
    		MaxLen = LIS(N);
    		int T;
    		int sum = 1;
    		while(T = LIS(N)){
    			if(T == MaxLen)sum++;
    			else break;
    		}
    		printf("%d
    %d
    ",MaxLen,sum);
    	}
    	
    	return 0;
    } 

  • 相关阅读:
    vue-router的push和replace的区别
    ajax请求常见状态码以及产生的原因
    vue定义data的三种方式与区别
    button与input button区别
    变量的声明方式
    js变量
    JavaScript的节流与防抖?
    js实现继承的方法-构造函数
    前端表单验证常用的15个JS正则表达式
    ES6中的新增数组的方法
  • 原文地址:https://www.cnblogs.com/vocaloid01/p/9514140.html
Copyright © 2011-2022 走看看