HDU 5748 Bellovin(求最大上升子序列的长度)
2016-08-12 19:30
246 查看
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
Sample Output
题意:
给出一个序列,求出以每个元素结尾的最长子序列的长度,然后找一个字典序最小的这个函数值相同的子序列。
思路:
定义一个数组,初始化都为最大值。再循环输入的数。找到数组中第一个比a大的数,记录下标即可。求完这个值后这个值构成的序列就是这个字典序最小的序列了;
求以a结尾的最长上升子序列的长度用k=lower_bound(dp,dp+n,a)-dp,
代码:
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int dp[100010];
#define INF (1<<31-1)//定义INF为0x3f3f3f时会wa;以后还是定义成这个吧;
int main()
{
int t,n;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(int i=0;i<n;i++)
{
dp[i]=INF;
}
for(int j=0;j<n;j++)
{
int a;
scanf("%d",&a);
int k=lower_bound(dp,dp+n,a)-dp;//找到以a为结尾的最长递增子序列的长度;
if(j==0)
{
printf("%d",k+1);//直接输出长度,要+1因为dp是从0开始的;
}
else
{
printf(" %d",k+1);
}
dp[k]=a;//把找过的这个数放到dp的这个位置中以便下一次寻找另一个数;
}
printf("\n");
}
return 0;
}
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
题意:
给出一个序列,求出以每个元素结尾的最长子序列的长度,然后找一个字典序最小的这个函数值相同的子序列。
思路:
定义一个数组,初始化都为最大值。再循环输入的数。找到数组中第一个比a大的数,记录下标即可。求完这个值后这个值构成的序列就是这个字典序最小的序列了;
求以a结尾的最长上升子序列的长度用k=lower_bound(dp,dp+n,a)-dp,
代码:
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int dp[100010];
#define INF (1<<31-1)//定义INF为0x3f3f3f时会wa;以后还是定义成这个吧;
int main()
{
int t,n;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(int i=0;i<n;i++)
{
dp[i]=INF;
}
for(int j=0;j<n;j++)
{
int a;
scanf("%d",&a);
int k=lower_bound(dp,dp+n,a)-dp;//找到以a为结尾的最长递增子序列的长度;
if(j==0)
{
printf("%d",k+1);//直接输出长度,要+1因为dp是从0开始的;
}
else
{
printf(" %d",k+1);
}
dp[k]=a;//把找过的这个数放到dp的这个位置中以便下一次寻找另一个数;
}
printf("\n");
}
return 0;
}
相关文章推荐
- 最大上升子序列长度
- hdoj 1950 Bridging signals【二分求最大上升子序列长度】【LIS】
- 最大上升子序列长度
- HDOJ --1950 Bridging signal【利用二分法来求最大上升子序列长度】
- tyvj 1208 最长不下降子序列2 求序列b1,b2,b3,…,bm中所有长度(n)最大上升子序列的个数
- 动态规划-3003-序列的最大上升子序列
- LightOJ 1421 Wavio Sequence 最大上升子序列变形 双dp
- RQNOJ 合唱队形(LIS最大上升序列解析)
- 求最长上升子序列的长度
- PHP 求最大递增子序列长度
- dp专题 第十三题 最大上升子序列的和
- LIS(最长上升子序列长度)
- hdu 1087最大上升子序列的和
- hdoj1087Super Jumping! Jumping! Jumping!(上升序列求最大和)
- Lintcode 114.最大上升连续子序列
- HDU 1423 最大公共递增子序列长度
- 最长上升子序列(LIS)长度 O(nlogn)算法 hdu1950为例
- (字符串的处理4.7.16)POJ 1159 Palindrome(让一个字符串变成回文串需要插入多少个字符...先逆序,在减去公共子序列的最大长度即可)
- 算法-计算递增最大子序列长度(动态规划)
- 最大上升子序列-python