题目:http://acm.hdu.edu.cn/showproblem.php?pid=1257
贪心解法:
#include<stdio.h>
int a[3005],vist[3005];
int main()
{
int i,n,count,temp,j;
while(scanf("%d",&n)!=EOF)
{
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
vist[i] =0;
}
count = 0;
for(i=0;i<n;i++)
{
if(!vist[i])
{
temp = a[i];
count++;
vist[i] = 1;
for(j=i+1;j<n;j++)
{
if(a[j] <= temp && !vist[j] )
{
temp = a[j];
vist[j]=1;
}
}
}
}
printf("%d\n",count);
}
return 0;
}
求最长递增序列
#include<stdio.h>
int a[3005],dp[3005];
int main()
{
int i,n,res,j;
freopen("a.txt","r",stdin);
while(scanf("%d",&n)!=EOF)
{
for(i=0;i<n;i++)
scanf("%d",&a[i]);
res = -1;
for(i=0;i<n;i++)
{
dp[i] = 1;
for(j=0;j<i;j++)
if( a[i] > a[j] && dp[i] < dp[j]+1)
{
dp[i] = dp[j]+1;
if ( dp[i] > res)
res = dp[i];
}
}
printf("%d\n",res);
}
return 0;
}