UVA, 10684 the jackpot

Source: Internet
Author: User

Test instructions: First enter an n, representing the number of subsequent n to continue the input, to find the maximum number of sub-sequences and

Idea: Dynamic programming, Maximum subsequence and (maximum sub-segment and)

Maximal sub-sequence and transfer equation: F[i]=max (f[i-1]+date[i],date[i]) can be used directly ...

The code is as follows:

1#include <iostream>2#include <cstdio>3#include <algorithm>4 5 using namespacestd;6 7 intn,date[10000],f[10000];8 9 intCMP (Const void*a,Const void*b)Ten { One     return*(int*) a-* (int*) b; A } -  - BOOLdatecin () the { -     if(SCANF ("%d", &n)!=eof&&N) -     { -          for(intI=0; i<n;i++) +         { -scanf"%d",&date[i]); +f[i]=0; A         } at         return true; -     } -     return false; - } -  - voidSHOWF () in { -      for(intI=0; i<n;i++) tocout<<f[i]<<' '; +cout<<Endl; - } the  * voiddatecal () $ {Panax Notoginsengf[0]=date[0]; -      for(intI=1; i<=n;i++) the     { +         if(f[i-1]>0) Af[i]=f[i-1]+Date[i]; the         Else +         { -f[i]=Date[i]; $         } $     } -     //SHOWF (); -Qsort (F,n,sizeof(f[0]), CMP);//I used qsort right here. the     //cout<<f[0]<< ': ' <<f[n-1]<<endl; - }Wuyi  the voidshowres () - { Wu     if(f[n-1]>0) -printf"The maximum winning streak is%d.\n", f[n-1]); About     Else $printf"losing streak.\n"); - } - intMain () - { A      while(Datecin ()) +     { the datecal (); - showres (); $     } the     return 0; the}

The above code uses the Qsort function, is a sort very convenient function, first simple use, after careful study

Maximum subsequence and there are a variety of solutions, here I use the dynamic planning, other methods, also write a = = Join the learning queue! Orz

UVA, 10684 the jackpot

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.