参考了别人的代码,自己也真正懂了的第一道应该算是DP吧。用一个数组来记录以每个数字为结尾时序数的个数情况。然后后面的数若比前面的数大并且序数个数大于以这个数为结尾的序数个数,则在前面的基础上加上1并保存结果。最后输出数组中的最大值!
i | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
num[i] | 1 | 7 | 3 | 5 | 9 | 4 | 8 |
ans[i] | 1 | 2 | 2 | 3 | 4 | 3 | 4 |
#include <stdio.h>
int main()
{
int ncases;
scanf("%d", &ncases);
while(ncases--)
{
int n, i, j, num[1005], max, ans[1005], result = 0;
scanf("%d", &n);
for(i = 1; i <= n; i++)
{
max = 0;
scanf("%d", &num[i]);
for(j = 1; j <= i; j++)
{
if(num[j] < num[i] && ans[j] > max)
max = ans[j];
}
ans[i] = max + 1;
if(ans[i] >result)
result = ans[i];
}
printf("%d\n", result);
if(ncases)
printf("\n");
}
return 0;
}