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. 使用ssh无密码登录

    使用ssh无密码登录 ssh 是一个专为远程登录会话和其他网络服务提供安全性的协议.默认状态下ssh链接是需要密码认证的,可以通过添加系统认证(即公钥-私钥)的修改,修改后系统间切换可以避免密码输入和 ...

  2. java之Set源代码浅析

    Set的接口和实现类是最简单的,说它简单原因是由于它的实现都是基于实际的map实现的. 如 hashSet 基于hashMap,TreeSet 基于TreeMap,CopyOnWriteArraySe ...

  3. c/c++测试程序运行时间

    算法分析中需要对各种算法进行性能测试,下面介绍两种通用的测试方法,由于只用到标准c语言函数,所以在各种平台和编译器下都能使用. 方法1: clock()函数 开始计时:start = clock() ...

  4. 转载ajax

    写在前面的话: 用了很久的Asp.Net Ajax,也看了段时间的jquery中ajax的应用,但到头来,居然想不起xmlHttpRequest的该如何使用了. 以前记的也不怎么清楚,这次就重新完整的 ...

  5. 替换Avada主题的Google字体

    刚玩WP的时候图省事,在themeforest买了排行第一的主题Avada,虽然强大,但对我目前的Blog应用而言实在太'重'了.而且老外的主题很多方面不接地气,比如谷歌字体.本文指导各位如何在Ava ...

  6. OC KVC总结

    在iOS开发中,我们一般使用set方法或者点语法来修改对象的属性值,比如说 stu.age = 9 与 [stu setAge:9]. KVC(key value coding)键值编码,这是一种间接 ...

  7. JavaSE_ Java基础 总目录(1~6)

    JavaSE学习总结第01天_Java概述01.01 计算机概述01.02 计算机硬件和软件概述01.03 软件开发和计算机语言概述01.04 人机交互01.05 键盘功能键和快捷键01.06 如何打 ...

  8. C++对象模型5--多继承下的对象模型

    C++对象模型中加入多继承 从单继承可以知道,派生类中只是扩充了基类的虚函数表.如果是多继承的话,又是如何扩充的? 1)        每个基类都有自己的虚表. 2)        子类的成员函数被放 ...

  9. 我用过的Linux命令--虚拟机和宿主机的网络连接方式

    VMWare提供了三种工作模式,它们是bridged(bridged模式:对应网卡vment0).NAT(网络地址转换模式:对应网卡vment8)和host-only(主机模式:对应网卡vment1) ...

  10. .net Web应用程序使用IIS调试

    1.这种调试方式是区别于使用Visual Studio 自带的调试方式 2.点击[创建虚拟目录],成功