Test instructions: For a sequence, to find a continuous sub-sequence of linear or equal ratio , to seek its longest length.
Idea: Sweep two times, judge the linear or equal. Swipe from left to right, maintain a sliding window, consider the newly added number, if the requirements are met, update the length, otherwise only the last two digits, the other deleted, and then continue to consider the next number. The same is true, just pay attention to the problem of precision.
Someone else's code:
1#include <bits/stdc++.h>2 using namespacestd;3 Const intMAX = 1e6+2;4 intArr[max];5 6 intMainvoid)7 {8 //freopen ("In.txt", "R", stdin);9 intt =0;Tenscanf"%d", &t); One while(t--) A { - intn =0; -scanf"%d", &n); the - for(intI=1; i<=n; ++i) - { -scanf"%d", &arr[i]); + } - + if(n==1|| n==2) A { atprintf"%d\n", n); - Continue; - } - - intL, R; -L =1; in intdiff = arr[2]-arr[1]; - intANS1 =2; to for(r=3; r<=n; ++R) + { - if(Arr[r]-arr[r-1] !=diff) the { *diff = arr[r]-arr[r-1]; $L = r1;Panax Notoginseng } -ANS1 = Max (ans1, r-l+1); the } + AL=1; the DoubleDdiff =1.0*arr[2]/arr[1]; + intAns2 =2; - for(r=3; r<=n; ++R) $ { $ if(Fabs (1.0*arr[r]/arr[r-1]-Ddiff) >0.000001) - { -Ddiff =1.0*arr[r]/arr[r-1]; theL = r1; - }WuyiANS2 = Max (Ans2, r-l+1); the } -printf"%d\n", Max (ans1, Ans2)); Wu } - return 0; About}
AC Code
HDU 5328 problem Killer (water problem)