The All-purpose Zero
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1618 Accepted Submission(s): 774
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.
#include <cstdio>
int b[100010];
int d[100010];
int ans;
int erfen(int x)
{
int l=1,r=ans;
int mid,pos;
while(l<=r)
{
mid=(l+r)/2;
if(d[mid]>x)
{
pos=mid;
r=mid-1;
}
else
{
l=mid+1;
}
}
return pos;
}
int main()
{
int t;
scanf("%d",&t);
int k=1;
while(t--)
{
int n;
scanf("%d",&n);
int num=0;//0的数量
int hao=0;//新数组角标
for(int i=1;i<=n;i++)//输入数据的同时构建新数组
{
int x;
scanf("%d",&x);
if(x==0)
{
num++;
}
else
{
hao++;
b[hao]=x-num;
}
}
if(hao==0)//全0的时候特判一下
{
printf("Case #%d: ",k++);
printf("%d\n",n);
continue;
}
d[1]=b[1];
ans=1;//记录LIS长度
for(int i=2;i<=hao;i++)
{
if(b[i]>d[ans])
{
ans++;
d[ans]=b[i];
}
else
{
d[erfen(b[i])]=b[i];
}
}
printf("Case #%d: ",k++);
printf("%d\n",ans+num);
}
return 0;
}
1696


&spm=1001.2101.3001.11974&articleId=52197978&d=1&t=3&u=7576374549c540798658cfc9e4fcb971)

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



