求最长单调递增【等差】子序列
2013-08-26 16:40
211 查看
Boring Tim
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 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 gradesare 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
41 3 2 4
5
11 22 44 33 55
0
Sample Output
22
思路:增加一维记录公差,然后再做最长求最长单调递增子序列即可。 这题输出的是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; }
相关文章推荐
- 最长单调递增子序列LIS
- [dp]最长单调递增子序列LIS
- 最长单调递增子序列
- 最长公共子序列LCS和最长单调递增子序列
- 最长单调"连续"递增子序列
- hdu 6197 贪心 最长单调递增子序列
- nyoj 17 数据结构 最长单调递增子序列
- NYOJ 17 最长单调递增子序列
- 设计一个O(n2)时间的算法,找出由n个数组成的序列的最长单调递增子序列。
- 单调最长递增子序列
- POJ1631最长单调递增子序列
- 最长单调递增子序列
- 用二分法寻找最长连续单调递增子序列
- 最长单调递增子序列
- 求最长单调递增子序列
- (p226)最长单调递增子序列
- 时间复杂度为O(nlogn)的最长单调递增子序列
- 程序员面试100题之十二:求数组中最长递增子序列
- 经典算法实现(最长公共子序列,最长递增子序列)
- 数组中最长递增子序列