"POJ" 3378 Crazy thairs (tree-like array +dp+ high-precision)

Source: Internet
Author: User

Topic Portal: QWQ

Analysis

Test instructions: Give a series, ask how many five Yuan rise group

Consider simplifying the question: if the topic asks for a two-dollar ascent group, what to do.

In reverse order, use a tree-like array to maintain the OK.

Ternary how to do it?

The expansion of the two yuan to one, that is, the third is also thrown into a tree array

So the problem is becoming clearer:

With $ dp[i][x] $ for $ a[x] $ to end the $ x $ $ Rise Group has how many

So:

$ dp[i][x]=\sum_{j=1}^{i-1} Dp[j][x-1] (A[j]<a[i]) $

Where $ dp[i][1]=1 $

Because there's a big one, plus one.

But this looks like $ O (n^2) $, definitely cool, so throw it into a tree-like array to optimize for a bit.

Well, the problem is also discrete and highly accurate.

Code
#include <cstdio>#include<algorithm>#include<cstring>using namespacestd;Const intmaxn=50010, N =50010, Base =10000000; typedefLong LongLL;classBignum { Public:      intnum[7], Len; Bignum (): Len (0) {} bignum (intN): Len (0) { for(; n >0; n/= Base) num[len++] = n%Base; } bignum bigvalueof (LL N) {len=0; while(n) {num[len++] = n%base; N/=Base; }          return* This; } bignumoperator+ (Constbignum&b) {bignum C; intI, carry =0;  for(i =0; I < This->len | | I < B.len | | Carry >0; ++i) {if(I < This->len) Carry + = This-Num[i]; if(I < B.len) Carry + =B.num[i]; C.num[i]= Carry%base; Carry/=Base; } C.len= i;returnC; } bignumoperator+= (Constbignum& b) {* This= * This+ b;return* This; }      voidPrint () {if(len = =0) {puts ("0");return ;} printf ("%d", Num[len-1]);  for(inti = len-2; I >=0; --i) for(intj = base/Ten; J >0; J/=Ten) printf ("%d", num[i]/j%Ten); Puts (""); }};typedef bignum bign;intn;bign sum[maxn][8]; structnode{intV,pos; BOOL operator< (Constnode& a)Const{returnv<a.v;}} A[MAXN];voidAddintXintE,bign a) { for(; x<=n;x+=x&-x) sum[x][e]+=A;} Bign Summ (intXinte) {bign ans; for(;x>0; x-=x&-x) ans+=sum[x][e];returnans;}intMain () { while(~SCANF ("%d",&N)) {         for(intI=1; i<=n;i++) {scanf ("%d", &AMP;A[I].V); a[i].pos=i;} Sort (a+1, A +1+N); memset (SUM,0,sizeof(sum)); intCnt=0; Bign ans=0;  for(intI=1; i<=n;i++) {Add (A[i].pos,1,1);  for(intj=2; j<=5; j + +) {Add (A[i].pos,j,summ (A[i].pos-1, J-1)); }} summ (N,5).    Print (); }    return 0;}

"POJ" 3378 Crazy thairs (tree-like array +dp+ high-precision)

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.