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. MD5的加密和解密(总结)

    效果图例如以下: package com.test; import java.security.MessageDigest; public class MD5 { // MD5加码.32位 publi ...

  2. cocos2d-x-3.1 Text Labels(官方正式译文)

    介绍 cocos2d支持(true type字体)标签,和纹理地图集标签. LabelTTF 标签的优缺点: 全部 TTF 字体的长处: 随意大小,支持调整字距. 易于使用. 不须要外部编辑器. 创建 ...

  3. sql 分割字符串 存储过程

    默认是用 , 分割,如有需要可以自己更改 我写的是 循环插入数据 存储过程,也可以自己改成 方法使用 存储过程: ),@jrid int) as begin declare @location int ...

  4. SQLite For .Net 已经整合了32位和64位

    以前引用SQLite.DLL的时候,如果是winform等桌面程序,还要分32位和64位不一样的DLL,但最近已经整合为一个包了 打开vs的程序包管理器控制器,输入: install-package ...

  5. BZOJ 1624: [Usaco2008 Open] Clear And Present Danger 寻宝之路( 最短路 )

    直接floyd.. ----------------------------------------------------------------------- #include<cstdio ...

  6. 加密PHP文件的方式,目测这样可以写个DLL来加密了

    <?php function encode_file_contents($filename) { $type=strtolower(substr(strrchr($filename,'.'),1 ...

  7. PhpStorm 10.0.1安装(win7-64位)

    软件下载地址 http://www.xiazaiba.com/html/25261.html#j_down_list 或者:http://pan.baidu.com/s/1brSA9C 密码:tpc7 ...

  8. Inlay技术要求

    物理特性: 项目 要求内容 备考 基准值 公差 INLAY尺寸 A(长) 480mm ±0.5mm B(宽) 380mm ±0.5mm 线圈位置 C(天地位置) 16.05mm ±0.2mm D(左右 ...

  9. android怎样自定义设置下拉列表样式

    图样: 实现方式: 1.水平布局一个TextView和一个ImageView(小黑箭头) 2.实现点击ImageView的单击事件,弹出PopupWindow 3.PopupWindow中实现下拉列表 ...

  10. js获取中英文长度

    function getLength(str) {    var len = str.length;    var reLen = 0;    for (var i = 0; i < len; ...