HDU5748-Bellovin【最長上升子序列的變形】


                     點擊打開鏈接



Sample Input
3 1 10 5 5 4 3 2 1 3 1 3 5
 

Sample Output
1 1 1 1 1 1 1 2 3

給一個序列,求出每個位置結尾的最長上升子序列;然後找一個字典序最小的這個函數值相同的子序列;


 
#include <cstdio>  
#include <cstring>
#include<algorithm>  
using namespace std;  
#define INF	0x3f3f3f3f  
int dp[100010];  
  
int main()  
{  
    int t;  
    scanf("%d",&t);  
    while (t--)  
    {  
        int n,a;  
        scanf("%d",&n);  
        for(int i=0; i<=n; i++)  
            dp[i]=INF;  
        for (int i=0; i<n; i++)  
        {  
            scanf("%d",&a);  
            int pos=lower_bound(dp,dp+n,a)-dp;	  
            printf (i==0?"%d":" %d",pos+1);  
            dp[pos]=a;  
            //printf("\ndp[%d]==%d\n",pos,dp[pos]);
        }  
        printf("\n");  
    }  
    return 0;  
}  












發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章