HDU 2227 Find the nondecreasing subsequences (线段树)
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
1 2 3
解题思路: 每进来一个数,方法数是原来所有小于等于它的数的方法数之和+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 (线段树)的更多相关文章
- HDU 2227 Find the nondecreasing subsequences (DP+树状数组+离散化)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2227 Find the nondecreasing subsequences ...
- HDU 2227 Find the nondecreasing subsequences(DP)
Problem Description How many nondecreasing subsequences can you find in the sequence S = {s1, s2, s3 ...
- HDU 2227 Find the nondecreasing subsequences (数状数组)
Find the nondecreasing subsequences Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/3 ...
- HDU 2227 Find the nondecreasing subsequences dp思想 + 树状数组
http://acm.hdu.edu.cn/showproblem.php?pid=2227 用dp[i]表示以第i个数为结尾的nondecreasing串有多少个. 那么对于每个a[i] 要去找 & ...
- HDU 2227 Find the nondecreasing subsequences
题目大意:给定一个序列,求出其所有的上升子序列. 题解:一开始我以为是动态规划,后来发现离散后树状数组很好做,首先,c保存的是第i位上升子系列有几个,那么树状数组的sum就直接是现在的答案了,不过更新 ...
- hdu 5274 Dylans loves tree(LCA + 线段树)
Dylans loves tree Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Othe ...
- HDU 3074.Multiply game-区间乘法-线段树(单点更新、区间查询),上推标记取模
Multiply game Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tot ...
- HDU 1394 Minimum Inversion Number(线段树求最小逆序数对)
HDU 1394 Minimum Inversion Number(线段树求最小逆序数对) ACM 题目地址:HDU 1394 Minimum Inversion Number 题意: 给一个序列由 ...
- hdu 1556:Color the ball(线段树,区间更新,经典题)
Color the ball Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
随机推荐
- 基于ZooKeeper的Dubbo简单抽样登记中心
一:设备zookeeper 系统环境 Ubuntu 14.04.2 LTS x64 IP : 192.168.1.102 下载zookeeper-3.4.6.tar.gz到文件夹/opt.拉开拉链 m ...
- .NET连接SAP系统专题:.NET调用RFC几种方式(一)
本来今天是要写一篇关于NCO3.0的东西,就是关乎.NET调用SAP的RFC的,支持VS2010和.NET 4.0等.现在网上到处都是充斥着NCO1.X和NCO2.0,需要用VS2003来使用,都是一 ...
- 防止 XSS 攻击 解决方案
XSS又叫CSS英文缩写为Cross Site Script中文意思为跨站脚本攻击具体内容指的是恶意攻击者往Web页面里插入恶意html代码,当用户浏览该页之时,嵌入其中Web里面的html代码会被执 ...
- webservice实例
前言:朋友们开始以下教程前,请先看第五大点的注意事项,以避免不必要的重复操作. 本文引自:http://www.iteye.com/topic/1135747 一.准备工作(以下为本实例使用工具) 1 ...
- MFC下DLL编程(图解)
DLL(Dynamic Link Library,动态链接库)是微软公司为Windows和OS/2操作系统设计一种供应用程序在运行时调用的共享函数库.DLL是应用程序的一种扩展,也是软件共享和重用的传 ...
- Vector(容器)
vector(容器)就像数组一样,但比数组强大很多,下面介绍一下vector常用的几种方法: 一.对于vector自身的处理,包括赋初始值,复制等等: vector<int> v1 ; v ...
- C++内联函数、函数模板之于头文件
一.基本说明 C++标准中提到,一个编译单元是指一个.cpp文件以及它所include的所有.h文件,.h文件里的代码将会被扩展到包含它的.cpp文件里,然后编译器编译该.cpp文件为一个.obj文件 ...
- Windows下安装Apache2.4+PHP5.4+Mysql5.7
注:文中所写的安装过程均在Win7 x86下通过测试,提供的百度云下载链接均为32位安装包,如需Apache和PHP的64位安装包请从官网下载! 一.安装Apache2.4.12 Apache官方下载 ...
- 我的Python成长之路---第一天---Python基础(4)---2015年12月26日(雾霾)
五.数据运算与数据运算符 1.算术运算符 算术运算符 运算符 描述 示例 + 加法 >>> 14 - 5 9 - 减法 >>> 14 - 5 9 * 乘法 &g ...
- Asp.net Role manager tutorial
It is very useful in .net we can user framework provided role manager, and easily configure in Web.C ...