Xjoi Online synchronization Training DAY1 T3

Source: Internet
Author: User

Idea: At the beginning to see this question when want DP, but found seemingly not. Because there is a prefix and suffix, and some suffixes will overwrite the current prefix, this does not satisfy the no-no effect of the AH!

But there is a very ingenious idea: if we know the maximum value of a[i, then the number of P and the number of Q are also determined. So the sequence length is also determined, set m for the sequence length.

And for each a[i] represents a fixed number of p and Q and lengths.

Therefore, the prefix length is greater than M/2, we can subtract it with the total p and the total Q, and convert it to a prefix suffix less than or equal to the M/2 length.

So we can design DP for F[I][J][K], representing a J p from left to right I, and a k p for the left I, so the position of f[(m+1)/2] is the final answer!

Note that a pre array is recorded to record where the optimal solution for this state is transferred from.

1#include <algorithm>2#include <cstdio>3#include <cmath>4#include <cstring>5#include <iostream>6 #definell Long Long7ll a[200005];8 Constll pw=9705276;9 Constll qw=12805858;Ten intc[200005][2],n,pre[205][205][205][2],w[205][205],f[205][205][205]; One intans[200005],CN; A intRead () { -     CharCh=getchar ();intt=0, f=1; -      while(ch<'0'||'9'<ch) {if(ch=='-') f=-1; ch=GetChar ();} the      while('0'<=ch&&ch<='9') {t=t*Ten+ch-'0'; ch=GetChar ();} -     returnt*F; - } - voidinit () { +n=read (); -      for(intI=0; i<n;i++){ +         DoubleX;SCANF ("%LF",&x); AA[i]= (LL) (x*100000+0.5); at     } - } - voidsolve () { -     intmxpos=0; -      for(intI=1; i<n;i++){ -         if(A[i]>a[mxpos]) mxpos=i; in     } -     inttotp=-1, totq=-1; to      for(intI=0;(ll) i*pw<=a[mxpos];i++) +      if(a[mxpos]-(LL) I*PW)%qw==0){ -totp=i; theTotq= (int) ((a[mxpos]-(LL) I*PW)/QW); *              Break; $     }Panax Notoginseng      for(intI=0; i<n;i++){ -         intp=-1, q=-1; the          for(intj=0;(ll) j*pw<=a[i];j++) +          if(a[i]-(LL) J*PW)%qw==0){ Ap=J; theQ= (int) ((a[i]-(LL) J*PW)/QW); +                  Break; -          } $          if(p!=-1&&p+q<=totp+TOTQ) { $c[cn][0]=p; -c[cn][1]=Q; -cn++; the          } -     }Wuyi     intm=totp+TOTQ; the      for(intI=0; i<cn;i++){ -         if(c[i][0]+c[i][1]<=m/2){ Wuw[c[i][0]+c[i][1]][c[i][1]]++; -}Else{ Aboutw[m-c[i][0]-c[i][1]][totq-c[i][1]]++; $         } -     } -f[0][0][0]=0; -      for(intI=1; i<=m/2; i++) A       for(intj=0; j<=i;j++) +        for(intk=0; k<=i;k++){ the             ints=-1; -              for(intp=0;p <=1;p + +){ $                  for(intq=0; q<=1; q++){ the                     if(j-p>=0&&j-p<i&&k-q>=0&&k-q<i&&f[i-1][j-p][k-q]>R) { thes=f[i-1][j-p][k-Q]; thepre[i][j][k][0]=p; thepre[i][j][k][1]=Q; -                     } in                 } the             } thef[i][j][k]=s+w[i][j]+ (k==j)?0: W[i][k]); About       } the     intansi=-1, ansj=-1, ansk=-1; the      for(intk=0; k<=m%2; k++) the       for(intI=0; i<=m/2; i++){ +         intj=totq-k-i; -         if(j>=0&&j<=m/2&& (ansi==-1|| f[m/2][i][j]>f[m/2][ANSI][ANSJ])) { theAnsi=i;Bayiansj=J; theansk=K; the         } -     } -     if(m%2) ans[m/2]=Ansk; the      for(inti=m/2;i>0; i--){ the         intp=pre[i][ansi][ansj][0]; the         intq=pre[i][ansi][ansj][1]; theans[i-1]=p; -ans[m-i]=Q; theansi-=p; theansj-=Q; the     }94      for(intI=0; i<m;i++) the      if(Ans[i]) printf ("Q"); the      Elseprintf"P"); the }98 intMain () { About init (); - solve ();101}

Xjoi Online synchronization Training DAY1 T3

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.