UVa 11039-building Designing

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.