Title: There is a number of n absolute value not 0, from which a sequence is found, plus or minus alternating, absolute value increment, the maximum length of the sequence.
Analysis: DP, dynamic programming. Because absolute values are incremented, they are sorted by absolute value first.
The sequence with the largest number of pre-set K is a maximum of f (k), then the following relations are pushed:
F (k) = f (k-1) {data[k]*data[k-1] > 0, the last quantity element is different when taken}
= f (k-1) + 1 {data[k]*data[k-1] < 0, last element simultaneous fetch}
(All data are not the same, and not 0)
Description: (⊙v⊙).
#include <algorithm> #include <iostream> #include <cstdlib> #include <cstring> #include < Cstdio> #include <cmath>using namespace Std;int data[500005],f[500005];bool cmp (int a, int b) {return ABS (a ) < ABS (b);} int main () {int p,n;while (~scanf ("%d", &p)) while (P--) {scanf ("%d", &n), for (int i = 0; i < n; + + i) scanf ("%d ", &data[i]); Sort (data, data+n, CMP); f[0] = 1;if (!n) f[0] = 0;for (int i = 1; I < n; + + i) if (1.0*data[i]*data[i-1] > 0) f[i] = F[i-1];else F[i] = f[i-1]+1;printf ("%d\n", F[n-1]);} return 0;}
UVa 11039-building Designing