Problem Link: UVA11039 Building designing. basic level exercises, written in C + + language program.
Test Instructions Description: Enter n absolute values of unequal 0 integers, select as many as possible, in a sequence, so that the positive and negative alternating and absolute value increment. Output its maximum length.
Problem analysis: After sorting by absolute value, look it over.
The AC C + + language program is as follows:
/* UVA11039 Building Designing * * #include <iostream> #include <cstdio> #include <algorithm>using namespace Std;const int maxn = 500000;int Val[maxn];int cmp (int& A, int& b) { return abs (a) < ABS (b);} int main () { int p, n, ans, I; scanf ("%d", &p); while (p--) { scanf ("%d", &n); for (i=0; i<n; i++) scanf ("%d", &val[i]); Sort (val, val+n, CMP); ans = 1; for (I=1; i<n; i++) { if (Val[i-1] > 0 && val[i] < 0) | | (Val[i-1] < 0 && Val[i] > 0)) ans++; } printf ("%d\n", ans); } return 0;}
UVA11039 Building Designing