Find the nondecreasing subsequences

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

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
 
Recommend
lcy
 

解题思路: 每进来一个数,方法数是原来所有小于等于它的数的方法数之和+1,然后再把这个数添加进为原来的数,下面依次循环。

100000个数,但是数值比较大,所以离散化一下,线段树来维护,每次lg的操作

因此总效率 O(n^lgn)

#include <iostream>
#include <cstdio>
#include <map>
#include <algorithm>
using namespace std; const int maxn=100010;
const int yu= 1000000007;
int n;
unsigned int data[maxn]; map <unsigned int,int> mp; struct node{
int l,r;
unsigned c;
}a[maxn*4]; void build(int l,int r,int k){
a[k].l=l;a[k].r=r;a[k].c=0;
if(l<r){
int mid=(l+r)/2;
build(l,mid,2*k);
build(mid+1,r,2*k+1);
}
} void insert(int l,int r,int c,int k){
a[k].c=(a[k].c+c)%yu;
if(l<=a[k].l && a[k].r<=r) return;
else{
int mid=(a[k].l+a[k].r)/2;
if(l>=mid+1) insert(l,r,c,2*k+1);
else if(r<=mid) insert(l,r,c,2*k);
else{
insert(l,mid,c,2*k);
insert(mid+1,r,c,2*k+1);
}
}
} unsigned query(int l,int r,int k){
if(l<=a[k].l && a[k].r<=r){
return a[k].c;
}else{
int mid=(a[k].l+a[k].r)/2;
if(l>=mid+1) return query(l,r,2*k+1);
else if(r<=mid) return query(l,r,2*k);
else{
return (query(l,mid,2*k)+query(mid+1,r,2*k+1))%yu;
}
}
} void initial(){
mp.clear();
build(1,n,1);
} void input(){
int cnt=1;
map <unsigned int,int>::iterator it;
for(int i=1;i<=n;i++){
scanf("%d",&data[i]);
mp[data[i]]=0;
}
for(it=mp.begin();it!=mp.end();it++){
it->second=cnt++;
}
} void computing(){
int ans=0,tmp,pos;
for(int i=1;i<=n;i++){
pos=mp[data[i]];
tmp=query(1,pos,1)+1;
ans=(ans+tmp)%yu;
insert(pos,pos,tmp,1);
}
cout<<ans<<endl;
} int main(){
while(scanf("%d",&n)!=EOF){
initial();
input();
computing();
}
return 0;
}

HDU 2227 Find the nondecreasing subsequences (线段树)的更多相关文章

  1. HDU 2227 Find the nondecreasing subsequences (DP+树状数组+离散化)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2227 Find the nondecreasing subsequences             ...

  2. HDU 2227 Find the nondecreasing subsequences(DP)

    Problem Description How many nondecreasing subsequences can you find in the sequence S = {s1, s2, s3 ...

  3. HDU 2227 Find the nondecreasing subsequences (数状数组)

    Find the nondecreasing subsequences Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/3 ...

  4. HDU 2227 Find the nondecreasing subsequences dp思想 + 树状数组

    http://acm.hdu.edu.cn/showproblem.php?pid=2227 用dp[i]表示以第i个数为结尾的nondecreasing串有多少个. 那么对于每个a[i] 要去找 & ...

  5. HDU 2227 Find the nondecreasing subsequences

    题目大意:给定一个序列,求出其所有的上升子序列. 题解:一开始我以为是动态规划,后来发现离散后树状数组很好做,首先,c保存的是第i位上升子系列有几个,那么树状数组的sum就直接是现在的答案了,不过更新 ...

  6. hdu 5274 Dylans loves tree(LCA + 线段树)

    Dylans loves tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Othe ...

  7. HDU 3074.Multiply game-区间乘法-线段树(单点更新、区间查询),上推标记取模

    Multiply game Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tot ...

  8. HDU 1394 Minimum Inversion Number(线段树求最小逆序数对)

    HDU 1394 Minimum Inversion Number(线段树求最小逆序数对) ACM 题目地址:HDU 1394 Minimum Inversion Number 题意:  给一个序列由 ...

  9. hdu 1556:Color the ball(线段树,区间更新,经典题)

    Color the ball Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

随机推荐

  1. 编译器DIY——词法分析

    在上一篇文章中已经介绍了读文件的操作,那么这一篇文章中将会细致解释词法分析. 在源文件里解析出的单词流必须识别为保留字,标识符,常量,操作符和界符五大类 1.显然我们须要列举出全部的保留字,而这里与保 ...

  2. Android内存泄漏分析及调试

    尊重原创作者,转载请注明出处: http://blog.csdn.net/gemmem/article/details/13017999 此文承接我的另一篇文章:Android进程的内存管理分析 首先 ...

  3. ios即时通讯客户端开发之-mac上基于XMPP的聊天客户端开发环境搭建

    1.搭建服务器  -  安装顺序 - (mysql->openfire->spark) 数据库:mysql 服务器管理工具: openfire 测试工具: spark mysql 安装 h ...

  4. 各种输入速度比较(cin,scanf,getchar,pascal的read)

    每个程序都是纯读入与1个输出.输入数据分别有1e2,1e3,1e4,1e5,1e6,1e7个数. C++使用getchar()读入是最快的.流输入是非常非常慢! 比赛还是用gechar比较好...

  5. 10条PHP编程习惯

    过去的几周对我来说是一段相当复杂的经历.我们公司进行了大裁员,我是其中之一,但却体验到了其中的乐 趣.我从来没有被开除过,所以很难不去想得太多.我开始浏览招聘板块,一个全职PHP程序员的职位很吸引人, ...

  6. codeforcese 498C. Array and Operations 网络流

    题目链接 给n个数, m个数对, 每个数对是两个下标加起来为奇数的两个数.每次操作可以使一个数对中的两个数同时除某个数, 除的这个数是这两个数的任意约数, 问这种操作最多可以做几次.n<100, ...

  7. [LeetCode]题解(python):109-Convert Sorted List to Binary Search Tree

    题目来源: https://leetcode.com/problems/convert-sorted-list-to-binary-search-tree/ 题意分析: 给定一个排好序的链表,将这个链 ...

  8. [LeetCode]题解(python):099-Recover Binary Search Tree

    题目来源: https://leetcode.com/problems/recover-binary-search-tree/ 题意分析: 二叉搜索树中有两个点错了位置,恢复这棵树. 题目思路: 如果 ...

  9. 解决用户 'IIS APPPOOL\Classic .NET AppPool' 登录失败

    解决用户 'IIS APPPOOL\Classic .NET AppPool' 登录失败 windows 7进入iis管理器 本地应用程序池 选中classic. net appPool 选择右侧的 ...

  10. 怪兽z主机标准版视频测试。

    我的淘宝店很早就开张了,但是一直没有好好打理,这次给大家带来的是本店所售一款主机的视频测试. CPU:AMD -A10 6700 主板:映泰Hi-Fi A88S3E 内存条:正品金士顿骇客游戏神条 机 ...