HUST 1377 - Sequence

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 that
each 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;
}
















































评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值