hdu 2227 Find the nondecreasing subsequences(树状数组)
来源:互联网 发布:开发人员从事数据分析 编辑:程序博客网 时间:2024/06/06 17:08
给出一个长度为n的序列求这个序列中非递减的序列有几个。
1,2,3: {1}, {2}, {3}, {1, 2}, {1, 3}, {2, 3}, {1, 2, 3}.一共有7个。
1 <= n <= 100000, 0 <= si <= 2^31.数据过大,所以需要离散化。
怎么离散化呢?将值映射到下标。再取原数组可知每个数在什么位置。
再将得到的下标查询,查询后+1,每次都是增加1,再将这个数更新进树状数组。说的不是很清楚,还是看代码吧
/*Problem ID:meaning:Analyzing:*/#include <iostream>#include <algorithm>#include<cstdio>#include<cmath>#include<cstdlib>#include<cstring>#include<vector>using namespace std;typedef struct even{int y1,y2,x;}even;#define clr(A,k) memset(A,k,sizeof(A))#define FOR(i,s,t) for(int i=(s); i<(t); i++)#define LL long long#define BUG puts("here!!!")#define print(x) printf("%d\n",x)#define STOP system("pause")#define eps 1e-8#define PI acos(-1.0)#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1#define maxn 100005#define maxm 1005#define MM 1<<31#define MOD 1000000007#define lowbit(x) x&(-x)LL gcd(LL a,LL b) {return a?gcd(b%a,a):b;}int S[maxn],C[maxn],A[maxn];int n,tot;void update(int pos,int val){ while(pos<=n){ C[pos]+=val; if(C[pos]>=MOD) C[pos]%=MOD; pos+=lowbit(pos); }}LL query(int x){ LL ret=0; while(x>0){ ret+=C[x]; if(ret>=MOD) ret%=MOD; x-=lowbit(x); } return ret;}int Binsearch(int x){ int l=1,r=tot; int m; while(l<=r){ int m=(l+r)>>1; if(S[m]==x) return m; if(S[m]>x) r=m-1; else l=m+1; }}int main(){ while(~scanf("%d",&n)){ clr(C,0); for(int i=1;i<=n;i++){ scanf("%d",&A[i]); S[i]=A[i]; } sort(S+1,S+n+1); tot=1; for(int t=2;t<=n;t++){ if(S[t]!=S[t-1]){ S[++tot]=S[t]; } } LL ret=0; for(int i=1;i<n+1;i++){ int id=Binsearch(A[i]); LL tp=query(id); ret+=tp+1; if(ret>=MOD) ret%=MOD; update(id,tp+1); } printf("%I64d\n",ret); }return 0;}
- hdu 2227 Find the nondecreasing subsequences(树状数组)
- HDU 2227 Find the nondecreasing subsequences 树状数组
- hdu 2227 Find the nondecreasing subsequences 树状数组+简单DP
- hdu 2227 Find the nondecreasing subsequences(树状数组+排序)
- hdu 2227 Find the nondecreasing subsequences(树状数组+DP)
- hdu-Find the nondecreasing subsequences(树状数组)
- 树状数组 hdu2227 Find the nondecreasing subsequences
- Find the nondecreasing subsequences--(树状数组)
- hdoj 2227 Find the nondecreasing subsequences 【树状数组优化dp】
- hdu 2227 Find the nondecreasing subsequences【离散化+树状数组+DP思路】
- hdu 2227 Find the nondecreasing subsequences (树状数组+dp+离散化)
- HDU - 2227 Find the nondecreasing subsequences (树状数组 + 子序列 + 离散化)
- hdu 2227Find the nondecreasing subsequences(树状数组+dp+离散化)
- hdu-2227-Find the nondecreasing subsequences(DP+离散化+树状数组)
- hdu 2227 Find the nondecreasing subsequences
- Find the nondecreasing subsequences HDU 2227
- HDU 2227 Find the nondecreasing subsequences
- HDU - 2227 Find the nondecreasing subsequences
- 海量数据存储
- Spring+IBatis整合各层设计(I)
- 排序算法总结
- Vsftpd 软件包的安装
- python简单的使用(二)
- hdu 2227 Find the nondecreasing subsequences(树状数组)
- 常用控件的缩写记录
- win7中phpmyadmin的配置
- 5个Servlet过滤器实例源码(JSP)
- 位运算符
- hdu 4405 Aeroplane chess(很水的期望DP)
- string,char,char*,char a[] 占字节数, 以及sizeof,strlen(),str.length()的用法
- Android初步学代码收集
- 移植ZC301摄像头到linux下