求序列中滿足Ai < Aj > Ak and i < j < k的組數 樹狀數組 HIT 2275 Number sequence,aj2275

來源:互聯網
上載者:User

求序列中滿足Ai < Aj > Ak and i < j < k的組數 樹狀數組 HIT 2275 Number sequence,aj2275

http://acm.hit.edu.cn/hoj/problem/view?id=2275

Number sequence
  Source : SCU Programming Contest 2006 Final
  Time limit : 1 sec   Memory limit : 64 M

Submitted : 1632, Accepted : 440

Given a number sequence which has N element(s), please calculate the number of different collocation for three number Ai, Aj, Ak, which satisfy that Ai < Aj > Ak and i < j < k.

Input

The first line is an integer N (N <= 50000). The second line contains N integer(s): A1, A2, ..., An(0 <= Ai <= 32768).

Output

There is only one number, which is the the number of different collocation.

Sample Input
51 2 3 4 1
Sample Output
6
  • Submit
  • Status
  • Statistic
  • Solution
  • Discuss
  • Print

解法一

1.正序依次按如下步驟處理每個資料:用樹狀數組統計之前比它小的有多少,並記錄在tmp數組裡;從此資料開始向後更新更大的數。對此我要解釋一下:樹狀數組的下標是數的大小,由於是按順序來的,所以保證了i<j<k。由此可知道前面比此數小的有多少個。

2.將c數組清空後倒敘處理一遍資料。由此可知道後面比此數小的有多少個。

3.將每個數的兩個資料相乘求和,最後用long long儲存輸出。

#include<stdio.h>#include<iostream>#include<string.h>#include<algorithm>#define MAXN 50010#define MAXM 32771 int a[MAXN];int c[MAXM];int tmp[MAXN];int n;int lowbit(int x){    return x&(-x);}void add(int x,int ad){    while(x<MAXM){    c[x]+=ad;        x+=lowbit(x);    }}int sum(int x){    int res=0;    while(x>0){        res+=c[x];        x-=lowbit(x);    }     return res;}int main(){    while(scanf("%d",&n)!=EOF){     long long ans=0;    memset(tmp,0,sizeof(tmp));    memset(c,0,sizeof(c));        for(int i=1;i<=n;i++){            scanf("%d",&a[i]);          a[i]++;           tmp[i]=sum(a[i]-1);   //注意是嚴格的大於            add(a[i],1);         }        memset(c,0,sizeof(c));        for(int i=n;i>=1;i--){        ans+=(long long)sum(a[i]-1)*tmp[i]; //(long long)很按理說重要,不過沒有加也過了。         add(a[i],1);        }        printf("%lld\n",ans);    }    return 0;}



解法二

1 題目要求的是總共的搭配方式,滿足Ai < Aj > Ak.並且i j k不同

2 我們開兩個樹狀數組,第一個在輸入的時候就去更新。然後我們在去枚舉Aj 同時維護第二個樹狀數組,對於AI來說就是在第二個樹狀數組裡面求和

然後在通過第一個樹狀數組就可以求出Ak的個數,把結果相乘即可

代碼:

#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespace std;const int MAXN = 50010;int n , num[MAXN];int treeNumOne[MAXN];int treeNumTwo[MAXN];int lowbit(int x){    return x&(-x);}int getSum(int *arr , int x){    int sum = 0;    while(x){        sum += arr[x];        x -= lowbit(x);    }    return sum;}void add(int *arr , int x , int val){    while(x < MAXN){         arr[x]  += val;          x += lowbit(x);    }}long long getAns(){    if(n < 3)        return 0;    long long ans = 0;    add(treeNumTwo , num[1] , 1);    for(int i = 2 ; i < n ; i++){        int x = getSum(treeNumTwo , num[i]-1);         int y = getSum(treeNumOne , num[i]-1);         add(treeNumTwo , num[i] , 1);        ans += (x)*(y-x);     }    return ans;}int main(){    while(scanf("%d" , &n) != EOF){         memset(treeNumOne , 0 , sizeof(treeNumOne));         memset(treeNumTwo , 0 , sizeof(treeNumTwo));          for(int i = 1 ; i <= n ; i++){             scanf("%d" , &num[i]);               num[i]++;             add(treeNumOne , num[i] , 1);         }         printf("%lld\n" , getAns());    }    return 0;}


著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.