http://acm.hdu.edu.cn/showproblem.php?pid=2227
离散化+树状数组
View Code
1 //2227
2 #include <cstdio>
3 #include <cstring>
4 #include <algorithm>
5 using namespace std;
6
7 const int N=100010,maxn=100000,md=1000000007;
8 int val[N],a[N];
9 __int64 c[N];
10 int lowbit(int x)
11 {
12 return x&(-x);
13 }
14 int bfind(int x,int l,int r)
15 {
16 while(l<r)
17 {
18 int m=(l+r)>>1;
19 if(x==val[m]) return m;
20 else if(x<val[m]) r=m;
21 else l=m+1;
22 }
23 return l;
24 }
25 int main()
26 {
27 int n;
28 while(~scanf("%d",&n))
29 {
30 for(int i=1;i<=n;i++)
31 {
32 scanf("%d",&a[i]);
33 val[i]=a[i];
34 }
35 sort(val+1,val+n+1);
36 int m=2;
37 for(int i=2;i<=n;i++)
38 if(val[i]!=val[i-1]) val[m++]=val[i];
39 memset(c,0,sizeof(c));
40 for(int k=1;k<=n;k++)
41 {
42 int x=bfind(a[k],1,m-1);
43 __int64 cnt=1;
44 for(int i=x;i>0;i-=lowbit(i)) cnt=(cnt+c[i])%md;
45 for(int i=x;i<m;i+=lowbit(i)) c[i]=(c[i]+cnt)%md;
46 }
47 int ans=0;
48 for(int i=m-1;i>0;i-=lowbit(i)) ans=((__int64)ans+c[i])%md;
49 printf("%d\n",ans);
50 }
51 return 0;
52 }