[关闭]
@tenlee 2015-07-31T11:39:17.000000Z 字数 969 阅读 1385

HDU 5328 Problem Killer(2015多校联合)

HDUOJ


题目链接

戳我

题目大意

一个序列 a, 求这个序列的 连续子序列 中最长的等差序列或者等比序列

样例解释

2 // T
5 //n
1 2 3 4 6 //n个数, 1,2,3,4是最长等差序列,长度为4
10 //n
1 1 1 1 1 1 2 3 4 5 //前6个1满足等差(比)序列,且长度最长是6

解题思路

暴力查询即可
此题坑点: 等比序列 的 公比 可能时 分数.

代码

  1. //Author LJH
  2. //www.cnblogs.com/tenlee
  3. #include <cstdio>
  4. #include <cstdlib>
  5. #include <cstring>
  6. #include <cctype>
  7. #include <cmath>
  8. #include <algorithm>
  9. #include <vector>
  10. #include <queue>
  11. #include <stack>
  12. #include <map>
  13. #define clc(a, b) memset(a, b, sizeof(a))
  14. using namespace std;
  15. const int inf = 0x3f;
  16. const int INF = 0x3f3f3f3f;
  17. const int maxn = 1e6+5;
  18. const double eps = 1e-6;
  19. int n;
  20. double a[maxn];
  21. int main()
  22. {
  23. int T;
  24. scanf("%d", &T);
  25. while(T--)
  26. {
  27. scanf("%d", &n);
  28. for(int i = 0; i < n; i++)
  29. {
  30. scanf("%lf", &a[i]);
  31. }
  32. double ap = a[1] - a[0];
  33. double gp = a[1] / a[0];
  34. int len1 = 1, len2 = 1;
  35. int malen1 = 1, malen2 = 1;
  36. for(int i = 1; i < n; i++)
  37. {
  38. if(abs(a[i] - a[i-1] - ap) < eps) len1++;
  39. else
  40. {
  41. ap = a[i] - a[i-1];
  42. len1 = 2;
  43. }
  44. malen1 = (malen1 > len1 ? malen1 : len1);
  45. if(abs(a[i] / a[i-1] - gp) < eps) len2++;
  46. else
  47. {
  48. gp = a[i] / a[i-1];
  49. len2 = 2;
  50. }
  51. malen2 = (malen2 > len2 ? malen2 : len2);
  52. }
  53. printf("%d\n", malen1 > malen2 ? malen1 : malen2);
  54. }
  55. return 0;
  56. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注