求最长单调递增【等差】子序列

Boring Tim

Time Limit: 1 Sec   Memory Limit: 128 MB
Submit: 7   Solved: 3
[ Submit][ STATUS][ Web Board]

Description

Tim is study in Children of Primary School of Hunan Agricultural University. In this summer vacation, he didn`t get any homework.And he can`t go to Internet cafes to play LOL game because of he isn`t an adult. He looked at his report card. He hopes his grades are rising. So that he want to delete some score. He call this report card is a good report. specially when these score are an arithmetic sequence, he will be very happy and call it a great report. Could you tell him to make the report to be a great report how many score he need delete at least?

Input

There are many tests.For each test the first line is an integer N(2<=N<=1,000),in the second line there are N integer Di(Di<=1,000). When the number N is zero the input is over and you needn`t del this test.

Output

A sample number which is the minimun number of delete.

Sample Input

4
1 3 2 4
5
11 22 44 33 55
0

Sample Output

2
2

思路增加一维记录公差,然后再做最长求最长单调递增子序列即可。  这题输出的是n-最长等差子序列长度。

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
using namespace std;

int dp[1002][1002];  // 第二维记录公差
int main()
{
     int i,j,n,a[1002];

     while(~scanf("%d",&n)&&n)
     {
         for(i=1;i<=n;i++)
          scanf("%d",&a[i]);
        for(i=1;i<=n;i++)
            for(j=1;j<1002;j++)
            {
                dp[i][j]=1;
            }
         int ans=1;
         for(i=1;i<=n;i++)
         {

            for(j=1;j<i;j++)
                if(a[i]>a[j])
                {
                    int d=a[i]-a[j];//计算公差
                   dp[i][d]=max(dp[i][d],dp[j][d]+1);

                   if(dp[i][d]>ans)ans=dp[i][d];
                }
         }
         cout<<"最长单调递增等差子序列:"<<ans<<endl;
     }
    return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值