hdu-2227-dp+bit

Find the nondecreasing subsequences

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2213    Accepted Submission(s): 858

Problem Description
How many nondecreasing subsequences can you find in the sequence S = {s1, s2, s3, ...., sn} ? For example, we assume that S = {1, 2, 3}, and you can find seven nondecreasing subsequences, {1}, {2}, {3}, {1, 2}, {1, 3}, {2, 3}, {1, 2, 3}.
 
Input
The input consists of multiple test cases. Each case begins with a line containing a positive integer n that is the length of the sequence S, the next line contains n integers {s1, s2, s3, ...., sn}, 1 <= n <= 100000, 0 <= si <= 2^31.
 
Output
For each test case, output one line containing the number of nondecreasing subsequences you can find from the sequence S, the answer should % 1000000007.
 
Sample Input
3
1 2 3
 
Sample Output
7
 
Author
8600
     
    令f(i)表示以第i个元素为结尾的非递减子序列的个数,有 f(i)=SUM{f(j) | j<i&&a[j]<=a[i]}。
        用BIT来维护f,C[x]表示所有的f总和,下标反映的就是a[i]得值,这样在求解f(i)=sum(a[i])就好了。
    注意到ai范围较大,离散化处理一下。
 
  
 #include<bits/stdc++.h>
using namespace std;
#define ULL unsigned long long
#define LL long long
LL mod=1e9+;
LL C[];
int N;
struct node{
int v,d;
bool operator<(const node& C)const{
if(v!=C.v) return v<C.v;
return d<C.d;
}
}a[];
bool cmp(node A,node B){return A.d<B.d;}
int main(){
int i,j;
while(scanf("%d",&N)==){
LL ans=;
for(i=;i<=N;++i) scanf("%d",&a[i].v),a[i].d=i;
sort(a+,a++N);
for(i=;i<=N;++i) a[i].v=i;
sort(a+,a++N,cmp);
for(i=;i<=N;++i){
LL tmp=;
for(int x=a[i].v;x>;x-=(x&-x)) (tmp+=C[x])%=mod;
for(int x=a[i].v;x<=N;x+=(x&-x)) (C[x]+=tmp)%=mod;
(ans+=tmp)%=mod;
}
printf("%lld\n",ans);
memset(C,,sizeof(C));
}
return ;
}
上一篇:学习OpenCV,GPU模块


下一篇:k8s服务发现和负载均衡(转)