您的位置:首页 > 其它

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

2013-08-26 16:40 211 查看

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: