Creation August題集時間之主(Timer),creationaugust

來源:互聯網
上載者:User

Creation August題集時間之主(Timer),creationaugust

時間之主
Timer.c/cpp/pas
TimerAugust掌握著無盡時間裡不同時間軸的平行世界.每一個瞬間都會有
新的世界被創造出來,其中一些世界被創造出來的根源就是已有生靈的信仰.無論
是尸魂界幻想鄉數位世界…只要有足夠的信仰,世界之心就會抓取這些信仰並用來構建成新的世界.不同世界有不同編號和不同信仰值.
很不幸,你被他選中了.現在你需要在2s很不幸,你被他選中了.現在你需要在2s內回答他的問題(對能掌控時間的 Timer 來說 2 秒鐘已經很長很長了),否則就要被丟到時間裂縫裡變成一個永遠靜止的藝術品了.
輸入 Input
第一行兩個數 n,k,n 表示世界數目,k 說明你要找 k 元逆序組數目.
接下來 n 行每行一個數,表示每個世界的信仰值.(不超過 int範圍且不重複)
輸出 Output
輸出那個數目.(對 10^9+7 模數)
資料範圍 Hint
對 100%的資料,1≤n≤10^5,1≤k≤10.(特殊的 k=1 時答案為 0)
對 30%的資料,1≤n≤10.
Sample Input
4 3
10 8 3 1
Sample Output
4
用無盡的歲月去尋的你

來分析一下這道題,題意十分的簡單,就是求k元逆序組。首先二元逆序對的求法是非常簡單的,那麼到k元,其實就是在前一元的基礎上,在加上1元,這樣也就多次調用歸併排序的過程就可以了。
附帶TA的代碼:

//orz CA#include<cstdio>#include<cstring>#include<algorithm>#include<cstdlib>#include<cmath>#include<iostream>#define Mod 1000000007using namespace std;int f[11][100005],tf[100005],a[100005],ta[100005],k,b[100005];int hash[100005];char * cp=(char *)malloc(2000000);inline void in(int &x){    while(*cp<'0'||*cp>'9')++cp;    x=0;    while(*cp>='0'&&*cp<='9')x=x*10+(*cp++^'0');}inline void merge(int l,int r){    if(l==r)return;    int m=l+r>>1;    merge(l,m),merge(m+1,r);    //printf("[%d,%d]\n",l,r);    int i=l,j=m+1,o=l,s=0;    for(;i<=m&&j<=r;++o)        if(a[i]<a[j]){            ta[o]=a[i];            tf[o]=(f[k][i]+s)%Mod;            //cout<<"Get:"<<a[i]<<" "<<s<<endl;            ++i;        }        else{            ta[o]=a[j],tf[o]=f[k][j];            s=(s+f[k-1][a[j]])%Mod;            ++j;        }    memcpy(ta+o,a+i,sizeof(int)*(m-i+1));    for(;i<=m;++i,++o){        tf[o]=(f[k][i]+s)%Mod;        //cout<<"Get:"<<a[i]<<" "<<s<<endl;    }    memcpy(ta+o,a+j,sizeof(int)*(r-j+1));    memcpy(tf+o,f[k]+j,sizeof(int)*(r-j+1));    memcpy(a+l,ta+l,sizeof(int)*(r-l+1));    memcpy(f[k]+l,tf+l,sizeof(tf[0])*(r-l+1));}int main(){    freopen("timer.in","r",stdin);    freopen("timer.out","w",stdout);    int n,i,K;    fread(cp,1,2000000,stdin);    in(n),in(K);    if(K==1){        puts("0");        return 0;    }    for(i=1;i<=n;++i){        in(b[i]);        f[1][i]=1;        hash[i]=b[i];    }    sort(hash+1,hash+n+1);    for(i=n;i;--i)b[i]=lower_bound(hash,hash+n+1,b[i])-hash;    for(k=2;k<=K;++k){        memcpy(a,b,sizeof(int)*(n+1));        merge(1,n);    }    int ans=0;    for(i=n;i;--i)ans=(ans+f[K][i])%Mod;    printf("%d\n",ans);}

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

聯繫我們

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