hdu_2227_Find the nondecreasing subsequences_树状数组,离散化
生活随笔
收集整理的這篇文章主要介紹了
hdu_2227_Find the nondecreasing subsequences_树状数组,离散化
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題目連接:http://acm.hdu.edu.cn/showproblem.php?pid=2227
題意:給你一個集合,讓你求遞增子序列有多少個,和樹狀數組求逆序對差不多,不過數據比較大,要離散化一下。
?
1 #include<cstdio> 2 #include<algorithm> 3 #define FFC(i,a,b) for(int i=a;i<=b;i++) 4 #define LL long long 5 using namespace std; 6 LL tree[100010]; 7 int maxn,mod=1000000007,l,r,t,len,mid; 8 int dt[100010],a[100010]; 9 void init(int n){maxn=n;for(int i=0;i<=maxn;i++)tree[i]=0;} 10 inline void add(int x,int v){for(int i=x;i<=maxn;i+=i&-i)tree[i]=(tree[i]+v)%mod;} 11 inline LL sum(int x){LL an=0;for(;x>0;x-=x&-x)an=(an+tree[x])%mod;return an;} 12 inline int lowb(int x){for(l=1,r=len;l<=r;)if(a[mid=(l+r)>>1]>=x)r=(t=mid)-1;else l=mid+1;} 13 int main(){ 14 int n; 15 while(~scanf("%d",&n)){ 16 FFC(i,1,n){ 17 scanf("%d",&dt[i]); 18 a[i]=dt[i];//離散化數組 19 } 20 sort(a+1,a+1+n); 21 len=unique(a+1,a+1+n)-a-1;//離散化 22 init(len); 23 LL ans=0; 24 FFC(i,1,n){ 25 lowb(dt[i]);//二分查找 26 int tt=sum(t); 27 add(t,tt+1); 28 ans=(ans+tt+1)%mod;//注意,一個數它本身也是一個遞增子列,所以要加一 29 } 30 printf("%lld\n",ans); 31 } 32 return 0; 33 } View Code?
?
轉載于:https://www.cnblogs.com/bin-gege/p/5696184.html
總結
以上是生活随笔為你收集整理的hdu_2227_Find the nondecreasing subsequences_树状数组,离散化的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: MySQL中in(常量列表)的执行计划
- 下一篇: 软件工程的意义