HDU-5748 Bellovin 【LIS(STL应用)】

16 篇文章 0 订阅
6 篇文章 0 订阅

E - Bellovin
Time Limit:3000MS Memory Limit:131072KB 64bit IO Format:%I64d & %I64u
Submit

Status
Description
Peter has a sequence and he define a function on the sequence – , where is the length of the longest increasing subsequence ending with .

Peter would like to find another sequence in such a manner that equals to . Among all the possible sequences consisting of only positive integers, Peter wants the lexicographically smallest one.

The sequence is lexicographically smaller than sequence , if there is such number from to , that for and .
Input
There are multiple test cases. The first line of input contains an integer , indicating the number of test cases. For each test case:

The first contains an integer – the length of the sequence. The second line contains integers .
Output
For each test case, output integers denoting the lexicographically smallest sequence.

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


  1. 题意:给定序列a1,a2,a2…..aN,f(a i)代表以a i为结尾元素的最大上升子序列长度,让你求出一个序列b1,b2….bN,使任意的f(a i)=f(b i),求出满足要求的最小序列并输出(b i>1,从前向后贪心满足最小的);
  2. 思路:要输出最小的,那第一个肯定是1,第二个就要看f(a2),如果f(a2)=1那么第二个肯定选1,如果不成立,那么为2,以此类推可以知道bi=f(bi)=f(ai),就是求出每一位的最长上升子序列的长度;
  3. 失误:用vector写不知道哪错了,指针一直不对,调试好久也找不出来,最后一急把代码调试的一删,没想到就对了,错误还能这样找;
  4. 代码如下:

#include<cstdio>
#include<vector>
using namespace std;

int main()
{
    int T,N,i,tem;  
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&N);
        scanf("%d",&tem);   vector<int> VEC,ord; vector<int>::iterator iter;
        VEC.push_back(tem); ord.push_back(1);  

        for(i=2;i<=N;++i)
        {
            scanf("%d",&tem);
            iter = lower_bound(VEC.begin(),VEC.end(),tem); 
            ord.push_back( (iter-VEC.begin())+1);//用队列可能会更方便 

            if(iter==VEC.end()) VEC.push_back(tem);
            else *iter = tem;  

        } 
        int k=0;
        for(iter=ord.begin();iter!=ord.end();++iter)
        {
            if(k!=0) printf(" "); k=1;
            printf("%d",*iter);
        }
        printf("\n");
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值