The All-purpose Zero
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1603 Accepted Submission(s): 770
For each case,the first line contains an interger n,which is the length of the array s.
The next line contains n intergers separated by a single space, denote each number in S.
2 7 2 0 2 1 2 0 5 6 1 2 3 3 0 0
Case #1: 5 Case #2: 5HintIn the first case,you can change the second 0 to 3.So the longest increasing subsequence is 0 1 2 3 5.
这里查询最长上升子序列用新的方法挺简单的:dp[ ]里的每个元素就是a[ i ]的最长上升子序列。
最长上升子序列的长度也可以用已知二分公式快速求出。
<span style="font-size:10px;">#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define INF 0x3f3f3f3f
#define CLR(a,b) memset(a,b,sizeof(a))
int dp[100010];
int main()
{
int u,ca=1;
scanf("%d",&u);
while(u--)
{
int n;
int a[100010];
scanf("%d",&n);
for(int i=0;i<n;i++)
scanf("%d",&a[i]);
int num=0,ant=0;
for(int i=0;i<n;i++)
{
if(a[i]==0)
{
num++;
continue; //继续,所以下面新序列a[i]数组元素数目减少
}
a[i]-=num;
a[ant++]=a[i]; //减去0后构成的新的序列
}
fill(dp,dp+ant,INF);
for(int i=0;i<ant;i++)
*lower_bound(dp,dp+ant,a[i])=a[i]; //构成一个新的数组dp[],这个数组里的元素是新的a[i]的最长上升子序列
printf("Case #%d: %d\n",ca++,lower_bound(dp,dp+ant,INF)-dp+num); //最长上升子序列长度+num就是结果
}
return 0;
}
</span>
578


&spm=1001.2101.3001.11974&articleId=52195567&d=1&t=3&u=738271659295421d826df83bc079e38c)

被折叠的 条评论
为什么被折叠?



