分析:这种题烂大街,n^2,然后数据结构优化下到nlogn,离散化
#include <cstdio> #include <cstring> #include <queue> #include <set> #include <map> #include <stack> #include <cstdlib> #include <algorithm> #include <vector> #include <cmath> using namespace std; typedef long long LL; typedef pair<int,int>pii; const int N=1e5+5; const int INF=0x3f3f3f3f; const int mod=1000000007; int p[N],n,a[N],c[N]; int cnt; void add(int x,int t){ for(int i=x;i<=cnt;i+=i&(-i)) c[i]=(c[i]+t)%mod; } int ask(int x){ if(x==0)return 0; int ans=0; for(int i=x;i>0;i-=i&(-i)) ans=(ans+c[i])%mod; return ans; } int main(){ while(~scanf("%d",&n)){ for(int i=1;i<=n;++i) scanf("%d",&a[i]),p[i]=a[i]; sort(p+1,p+1+n); cnt=unique(p+1,p+1+n)-p-1; memset(c,0,sizeof(c)); int ans=0; for(int i=1;i<=n;++i){ int pos=lower_bound(p+1,p+1+cnt,a[i])-p; int tmp=(ask(pos-1)+1)%mod; add(pos,tmp); ans=(ans+tmp)%mod; } printf("%d ",ans); } return 0; }