HDU 5328 Problem Killer(水题)

题意:给一个序列,要找一个等差或等比的连续子序列,求其最长的长度。

思路:扫两遍,判断等差或等比即可。从左往右扫,维护一个滑动窗口,考虑新加进来的数,如果满足了要求,则更新长度,否则只留最后两个数字,其他删掉,接着继续考虑下一个数字。等比也是如此,只是要注意精度的问题。

  别人的代码:

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 const int MAX = 1e6+2;
 4 int arr[MAX];
 5
 6 int main(void)
 7 {
 8     //freopen("in.txt", "r", stdin);
 9     int t = 0;
10     scanf("%d", &t);
11     while(t--)
12     {
13         int n = 0;
14         scanf("%d", &n);
15
16         for(int i=1; i<=n; ++i)
17         {
18             scanf("%d", &arr[i]);
19         }
20
21         if (n==1 || n==2)
22         {
23             printf("%d\n", n);
24             continue;
25         }
26
27         int l, r;
28         l = 1;
29         int diff = arr[2]-arr[1];
30         int ans1 = 2;
31         for(r=3; r<=n; ++r)
32         {
33             if (arr[r] - arr[r-1] != diff)
34             {
35                 diff = arr[r]-arr[r-1];
36                 l = r-1;
37             }
38             ans1 = max(ans1, r-l+1);
39         }
40
41         l=1;
42         double ddiff = 1.0*arr[2]/arr[1];
43         int ans2 = 2;
44         for(r=3; r<=n; ++r)
45         {
46             if ( fabs( 1.0*arr[r]/arr[r-1] - ddiff) > 0.000001)
47             {
48                 ddiff = 1.0*arr[r]/arr[r-1];
49                 l = r-1;
50             }
51             ans2 = max(ans2, r-l+1);
52         }
53         printf("%d\n", max(ans1, ans2));
54     }
55     return 0;
56 }

AC代码

时间: 2024-11-11 17:06:09

HDU 5328 Problem Killer(水题)的相关文章

HDU 5328(2015多校4)-Problem Killer(水题)

题目地址:HDU 5328 题意:在一个长度为n的序列中取出连续的k个数,让这k个数组成等差数列或者等比数列,问这样的k最大可以是多少. Ps:注意用double搞,因为等比数列求公比相除可能为小数. #include <stdio.h> #include <math.h> #include <string.h> #include <stdlib.h> #include <iostream> #include <sstream> #i

HDU 5328 Problem Killer(尺取法)

You are a "Problem Killer", you want to solve many problems. Now you have nn problems, the ii-th problem's difficulty is represented by an integer aiai (1≤ai≤1091≤ai≤109). For some strange reason, you must choose some integer ll and rr (1≤l≤r≤n1

hdu 5328 Problem Killer(杭电多校赛第四场)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5328 题目大意:找到连续的最长的等差数列or等比数列. 解题思路:1.等差等比的性质有很多.其中比较重要的一个就是解题关键:如a[i-2],a[i-1],a[i],a[i+1]这个序列.a[i-2],a[i-1],a[i]是等差数列,a[i-1],a[i],a[i+1]也是等差数列.那么a[i-2],a[i-1],a[i],a[i+1]就是等差数列.  2. 等比数列也是一样的~~只要根据这个性质就

HDU 5328 Problem Killer

题意:给一段序列,求连续的子序列中最长的等差数列或者等比数列的长度. 解法:O(n)的扫两遍一次判等差一次判等比就好了. 代码: #include<stdio.h> #include<iostream> #include<algorithm> #include<string> #include<string.h> #include<math.h> #include<limits.h> #include<time.h&

简单的dp hdu 数塔(水题)

数塔 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 21314    Accepted Submission(s): 12808 Problem Description 在讲述DP算法的时候,一个经典的例子就是数塔问题,它是这样描述的: 有如下所示的数塔,要求从顶层走到底层,若每一步只能走到相邻的结点,则经过的结点的数字之和最大是多少

hdu 2053 Switch Game 水题一枚,鉴定完毕

Switch Game Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 10200    Accepted Submission(s): 6175 Problem Description There are many lamps in a line. All of them are off at first. A series of op

hdu 2212 DFS(水题)

DFS Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 4923    Accepted Submission(s): 3029 Problem Description A DFS(digital factorial sum) number is found by summing the factorial of every digit

HDU 2090 算菜价 --- 水题

/* HDU 2090 算菜价 --- 水题 */ #include <cstdio> int main() { char s[105]; double a, b, sum = 0; while (scanf("%s", s)==1){ scanf("%lf%lf", &a, &b); a *= b; sum += a; } printf("%.1f\n", sum); return 0; }

HDU 2091 空心三角形 --- 水题

/* HDU 2091 空心三角形 --- 水题 */ #include <cstdio> int main() { int kase = 0; char ch; int h, t; //h表示高 while (scanf("%c", &ch) == 1 && ch != '@'){ scanf("%d", &h); if (kase++){ printf("\n"); } getchar(); if