Poj 3378 -- crazy thairs (tree array + dp + high precision) Data Structure Optimization DP

Source: Internet
Author: User

Crazy thairs
Time limit:3000 Ms   Memory limit:65536 K
Total submissions:6598   Accepted:1636

Description

These days, sepageris crazed on one problem named Crazy Thair. GivenN(1 ≤N≤ 50000) numbers, which are no more than 109, crazy Thair is a group of 5 numbers {I,J,K,L,M} Satisfying:

1. 1 ≤I<J<K<L<MN
2.AI<AJ<AK<Al<AM

For example, in the sequence {2, 1, 3, 4, 5, 7, 6}, there are four crazy Thair groups: {1, 3, 4, 5, 6}, {2, 3, 4, 5, 6}, {1, 3, 4, 5, 7} and {2, 3, 4, 5, 7 }.

Cocould you help sepr to count how many crazy thairs in the sequence?

Input

Input contains several test cases. Each test case begins with a line containing a numberN, Followed by a line containingNNumbers.

Output

Output The amount of crazy thairs in each sequence.

Sample Input

51 2 3 4 572 1 3 4 5 7 671 2 3 4 5 6 7

Sample output

1421

------------------Split line--------------------

Question:

Given n numbers, satisfy

1. 1 ≤ I <j <k <L <m ≤ n
2. AI <AJ <AK <Al <AM

Total number of solutions


Ideas:

DP [I] [J] indicates the number of solutions for the sequence with the ending length of J as I, DP [I] [J] = DP [I-1] [K], where a [k] Must be smaller than a [J]

Therefore, if you want to know the number of 5 tuples, you need to know the number of 4 tuples and the number of 3 tuples ........ when the length is 1, the number of solutions is 1

It is no longer appropriate to count the number of elements smaller than a [I] using a tree array.

The data range of each element is 9 to the power of 10, and the number given by the question is 50000. discretization is required.


And the combination of C (, 5) must exceed _ int64, so use high precision


#include<iostream>#include<cstring>#include<cstdio>#include<algorithm>#define M 50000+10#define ll long long#define local1using namespace std;int n,ans[50],f[M];ll c[4][M];struct node{    int x,index;    bool operator< (const node &A)const    {        return x<A.x;    }}a[M];void update(int x,ll v,int k){    for(int i=x;i<=n;i+=i&-i){        c[k][i]+=v;    }}ll getsum(int x,int k){    ll sum=0;    for(int i=x;i>0;i-=i&-i){        sum+=c[k][i];    }    return sum;}void add(ll s){    int t1[50],t2[50],k=0;    memset(t1,0,sizeof(t1));memset(t2,0,sizeof(t2));    while(s>0){        t2[k++]=s%10;        s/=10;    }    for(int i=0;i<50;++i){        t1[i]+=t2[i]+ans[i];        if(t1[i]>=10){            t1[i+1]=t1[i]/10;            t1[i]%=10;        }        ans[i]=t1[i];    }}int main(){#ifdef local    freopen("in.txt","r",stdin);    freopen("ou.txt","w",stdout);#endif // local    while(scanf("%d",&n)!=EOF){        memset(c,0,sizeof(c));memset(ans,0,sizeof(ans));        for(int i=0;i<n;++i){            scanf("%d",&a[i].x);            a[i].index=i;        }        sort(a,a+n);        int key=1;f[a[0].index]=1;        for(int i=1;i<n;++i){            if(a[i].x>a[i-1].x) f[a[i].index]=++key;            else f[a[i].index]=key;        }        for(int i=0;i<n;++i){            update(f[i],1,0);            for(int j=1;j<=3;++j){                update(f[i],getsum(f[i]-1,j-1),j);            }            add(getsum(f[i]-1,3));        }        int k=49;        for(;k>=0;--k){            if(ans[k]) break;        }        for(int i=k;i>=0;--i){            printf("%d",ans[i]);        }        printf("\n");    }    return 0;}




Poj 3378 -- crazy thairs (tree array + dp + high precision) Data Structure Optimization DP

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.