1377 - Sequence
Time Limit: 1s Memory Limit:128MB
Submissions: 430 Solved: 144
-
Description
-
Given a number sequence whose length is n, you can delete at most k numbers in the sequence.
After that you are asked to answer the maximum length of longest continuous subsequence thateach number in it is larger than its previous number(if has any)by one.
Input
-
There are multy testcases. For each case:
the first line are the integer n, k. 1 <= n <= 100, 0 <= k <= n.
the seconde line is n numbers.
Output
-
For each case output one number representing the maximum length of the subsequence.
Sample Input
-
6 3 2 4 6 5 2 1 4 4 6 3 5 2
Sample Output
-
2 1
仔细看题会发现题目不难,注意题目中描述(红字),剩下就可以直接深搜,注意维护好每个数就好了。
AC代码:
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <algorithm>
#include <string.h>
using namespace std;
int n,k,num[105],ans;
void dfs(int i,int del,int len)
{
int j;
if(del>k)
{
return;
}
for(j=i+1;j<=n;j++)
{
if(num[j]==num[i]+1)
{
dfs(j,del+j-i-1,len+1);
}
}
if(del<=k&&len>ans)
{
ans=len;
}
}
int main()
{
int i;
while(~scanf("%d%d",&n,&k))
{
for(i=1;i<=n;i++)
{
scanf("%d",&num[i]);
}
ans=-1;
for(i=1;i<=n;i++)
{
dfs(i,0,1);
}
printf("%d\n",ans);
}
return 0;
}