Two algorithms for Usaco 1.1.4 beads O (n)

Source: Internet
Author: User

Algorithms one: recursive recursion similar to recursion. (or something like recursion ^ (* ̄ (oo)  ̄) ^ Forget it's not important)

1 type2Node=Record3 Bl,br,rl,rr,sum:longint;4 Data:char;5 End;6 var7 Len,i,j,t,mmax:longint;8A:Array[0..705] ofnode;9 functionMax (x,y:longint): Longint;Ten begin One   ifX>y Thenexit (x) A   Elseexit (y); - End; - begin the Readln (len); -    fori:=1  toLen Do - read (a[i].data); -    fori:=1  toLen Do +a[i+len].data:=A[i].data; -  +    fori:=1  tolen*2  Do A    CaseA[i].data of at     'W':begina[i].bl:=a[i-1].bl+1; a[i].rl:=a[i-1].rl+1;End; -     'R': a[i].rl:=a[i-1].rl+1; -     'b': a[i].bl:=a[i-1].bl+1; -   End; -  -    fori:=len*2 Downto 1  Do in    CaseA[i].data of -     'W':begina[i].br:=a[i+1].br+1; a[i].rr:=a[i+1].rr+1;End; to     'R': a[i].rr:=a[i+1].rr+1; +     'b': a[i].br:=a[i+1].br+1; -   End; the  *    fori:=1  tolen*2  Do $   beginPanax NotoginsengA[i].sum:=max (a[i-1].bl,a[i-1].RL) +Max (A[I].BR,A[I].RR); -     ifA[i].sum>len Thena[i].sum:=Len; the     ifA[i].sum>mmax Then +mmax:=a[i].sum; A   End; the Writeln (Mmax); + End.//specific what to see Nocow Http://www.nocow.cn/index.php/USACO/beads thought a front

Algorithms: Do not post code, on the path ~http://www.nocow.cn/index.php/usaco/beads the idea of a back

Core idea: {In fact, there is no need to use dynamic programming, direct search can also reach O (n).
Put two of the same necklace in one, start from the beginning with two variables (variables) A, a and record the number of two color beads that can be collected from the left point to date,
The maximum value of the a+b appears in the way, when the color transformation will be B assigned to a can, the code is very concise. }

Good want to spit Groove write Bo good time ... But it is said to write a long write Bo can be written as Oi God, or take out to see "sword" time to write Bo better ^ (* ̄ (oo)  ̄) ^

Two algorithms for Usaco 1.1.4 beads O (n)

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.