hdu4000 && hrbust1625

來源:互聯網
上載者:User

標籤:

ikki的數字
Time Limit: 1000 MS Memory Limit: 32768 K
Total Submit: 22(12 users) Total Accepted: 9(8 users) Rating:  Special Judge: No
Description

ikki 近期對數字頗感興趣。如今ikki在紙上寫了連續的N個數字,每一個數字都是[1,N]之間隨意的一個數並且不反覆,即這串數字

是數字1~N的一個排列。數位序號從1到N,如今ikki想考你一下:

在這N個數字中能找出多少個3個數的組合滿足:num[x]<num[z]<num[y]且x<y<z,當中x,y,z為這三個數位下標。

Input
多組測試資料,第一行一個整數T表示測試資料的個數。

對於每組資料,第一行輸入一個整數N表示數列中數位個數(1<=N<=5000)

第二行輸入N個數字表示一個1~N的排列。


Output

對於每組資料,輸出”Case #k: p” ,k表示第k組例子,p表示滿足要求的3個數位組合數目,每組輸出佔一行。

因為結果可能比較大,結果需對100000007模數。

Sample Input
2
6
1 3 2 6 5 4
5 
3 5 2 4 1


Sample Output
Case #1: 10
Case #2: 1

Author

周洲@hrbust


隱藏著樹狀數組~~~根本沒看出來,事實上主要是沒思路,思路出來了才幹用樹狀數組求解

推斷滿足i<j<k且num[i]<num[k]<num[j]的總組數利用樹狀數組能夠求出一個數前面比它小的數的個數,進而能夠知道前面比它大的數的個數,總的比它大的個數減去前面比它大的個數等於後面比它大的個數,Cn2 = x*(x-1)/2;然後肯定要減去後面的全部組成的i<j<k且a[i]<a[j]<a[k]的個數;

  1. 能夠先求出(xyz,xzy)的總數量 
  2. 僅僅需出去x後面多少個比它大的個數n,C(n,2)就是了 
  3. 然後求出xyz的個數, 
  4. 對於a,求出比a小的個數low[a],比a大的個數high[a],low[a]*high[a]就是答案 
  5. 能夠藉助樹狀數組求上述個數 
注意了,總體求解!

並非說單獨考慮某個數

#include <iostream>#include <cstring>#include <cstdio>using namespace std;typedef long long LL;const int mod = 100000007;int T,n;int c[5010];int lowbit(int x){    return x & -x;}LL getsum(int x){    LL sum = 0;    while(x > 0)    {        sum += c[x];        x -= lowbit(x);    }    return sum;}void update(int x , int val){    while(x <= n)    {        c[x] += val;        x += lowbit(x);    }}int main(){#ifdef xxz    freopen("in.txt","r",stdin);#endif // xxz    cin>>T;    int Case = 1;    while(T--)    {        memset(c,0,sizeof(c));        cin>>n;        LL ans = 0;        for(int i = 1; i <= n; i++)        {            int temp;            cin>>temp;            update(temp,1);            LL presmaller = getsum(temp - 1);            LL prebigger = i-1 - presmaller;            LL totbigger = n - temp;            LL afterbigger = totbigger - prebigger;            ans -= presmaller * afterbigger;            if(afterbigger >= 2)            {                ans += afterbigger*(afterbigger - 1)/2;            }        }        cout<<"Case #"<<Case++<<": "<<ans%mod<<endl;    }    return 0;}

著作權聲明:本文博主原創文章,部落格,未經同意不得轉載。

hdu4000 &amp;&amp; hrbust1625

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.