LightOJ 1085(树状数组+离散化+DP,线段树)
Time Limit:3000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu
Description
An increasing subsequence from a sequence A1, A2 ... An is defined by Ai1, Ai2 ... Aik, where the following properties hold
- i1 < i2 < i3 < ... < ik and
- 2. Ai1 < Ai2 < Ai3 < ... < Aik
Now you are given a sequence, you have to find the number of all possible increasing subsequences.
Input
Input starts with an integer T (≤ 10), denoting the number of test cases.
Each case contains an integer n (1 ≤ n ≤ 105) denoting the number of elements in the initial sequence. The next line will contain n integers separated by spaces, denoting the elements of the sequence. Each of these integers will be fit into a 32 bit signed integer.
Output
For each case of input, print the case number and the number of possible increasing subsequences modulo 1000000007.
Sample Input
3
3
1 1 2
5
1 2 1000 1000 1001
3
1 10 11
Sample Output
Case 1: 5
Case 2: 23
Case 3: 7
题解:让求单调递增序列的个数;dp[i]=sum(dp[j])+1(j<i);由于数太大,需要离散化由于是单调的,所以当相等的时候树状数组要从大到小inset,以免对后面相等的造成影响,还可以用线段树写;
树状数组:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
#define mem(x,y) memset(x,y,sizeof(x))
#define SI(x) scanf("%d",&x)
#define PI(x) printf("%d",x)
#define P_ printf(" ")
const int MOD=;
typedef long long LL;
const int INF=0x3f3f3f3f;
const int MAXN=1e5+;
int dp[MAXN],a[MAXN],p[MAXN];
int N;
int lowbit(int x){return x&(-x);}
void insert(int x,int v){
while(x<=N){
dp[x]=(dp[x]+v)%MOD;
x+=lowbit(x);
}
}
int sum(int x){
int ans=;
while(x>){
ans=(ans+dp[x])%MOD;
x-=lowbit(x);
}
return ans;
}
int cmp(int x,int y){
if(a[x]!=a[y])return a[x]<a[y];
else return x>y;
}
int main(){
int T,kase=;
SI(T);
while(T--){
SI(N);
for(int i=;i<=N;i++)SI(a[i]),p[i]=i;
sort(p+,p+N+,cmp);
mem(dp,);
for(int i=;i<=N;i++){
insert(p[i],sum(p[i])+);
}
printf("Case %d: %d\n",++kase,sum(N));
}
return ;
}
线段树:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
#define mem(x,y) memset(x,y,sizeof(x))
#define SI(x) scanf("%d",&x)
#define PI(x) printf("%d",x)
#define P_ printf(" ")
const int MOD=;
typedef long long LL;
const int INF=0x3f3f3f3f;
const int MAXN=1e5+;
#define lson root<<1,l,mid
#define rson root<<1|1,mid+1,r
#define ll root<<1
#define rr root<<1|1
int tree[MAXN<<],p[MAXN],a[MAXN];
int ans=;
void pushup(int root){
tree[root]=(tree[ll]+tree[rr])%MOD;
}
void insert(int root,int l,int r,int flog,int v){
int mid=(l+r)>>;
if(l==flog&&r==flog){
tree[root]=v;
return;
}
if(mid>=flog)insert(lson,flog,v);
if(mid<flog)insert(rson,flog,v);
pushup(root);
}
void sum(int root,int l,int r,int L,int R){
int mid=(l+r)>>;
if(l>=L&&r<=R){
ans=(ans+tree[root])%MOD;
return;
}
if(mid>=L)sum(lson,L,R);
if(mid<R)sum(rson,L,R);
return;
}
int cmp(int x,int y){
if(a[x]!=a[y])return a[x]<a[y];
else return x>y;
}
int main(){
int T,N,kase=;
SI(T);
while(T--){
SI(N);
for(int i=;i<=N;i++)SI(a[i]),p[i]=i;
sort(p+,p+N+,cmp);
mem(tree,);
for(int i=;i<=N;i++){
ans=;
sum(,,N,,p[i]);
insert(,,N,p[i],ans+);
}
printf("Case %d: %d\n",++kase,tree[]);
}
return ;
}
LightOJ 1085(树状数组+离散化+DP,线段树)的更多相关文章
- [Usaco2014 Open Gold ]Cow Optics (树状数组+扫描线/函数式线段树)
这道题一上手就知道怎么做了= = 直接求出原光路和从目标点出发的光路,求这些光路的交点就行了 然后用树状数组+扫描线或函数式线段树就能过了= = 大量的离散+模拟+二分什么的特别恶心,考试的时候是想到 ...
- 【BZOJ3110】【整体二分+树状数组区间修改/线段树】K大数查询
Description 有N个位置,M个操作.操作有两种,每次操作如果是1 a b c的形式表示在第a个位置到第b个位置,每个位置加入一个数c 如果是2 a b c形式,表示询问从第a个位置到第b个位 ...
- bzoj4785:[ZJOI2017]树状数组:二维线段树
分析: "如果你对树状数组比较熟悉,不难发现可怜求的是后缀和" 设数列为\(A\),那么可怜求的就是\(A_{l-1}\)到\(A_{r-1}\)的和(即\(l-1\)的后缀减\( ...
- HDU - 1166 树状数组模板(线段树也写了一遍)
题意: 汉语题就不说题意了,用到单点修改和区间查询(树状数组和线段树都可以) 思路: 树状数组的单点查询,单点修改和区间查询. 树状数组是巧妙运用二进制的规律建树,建树就相当于单点修改.这里面用到一个 ...
- BZOJ 3110([Zjoi2013]K大数查询-区间第k大[段修改,在线]-树状数组套函数式线段树)
3110: [Zjoi2013]K大数查询 Time Limit: 20 Sec Memory Limit: 512 MB Submit: 418 Solved: 235 [ Submit][ ...
- BZOJ 4785 [Zjoi2017]树状数组 | 二维线段树
题目链接 BZOJ 4785 题解 这道题真是令人头秃 = = 可以看出题面中的九条可怜把求前缀和写成了求后缀和,然后他求的区间和却仍然是sum[r] ^ sum[l - 1],实际上求的是闭区间[l ...
- 2019.01.21 bzoj2441: [中山市选2011]小W的问题(树状数组+权值线段树)
传送门 数据结构优化计数菜题. 题意简述:给nnn个点问有多少个www型. www型的定义: 由5个不同的点组成,满足x1<x2<x3<x4<x5,x3>x1>x2 ...
- POJ 2299 【树状数组 离散化】
题目链接:POJ 2299 Ultra-QuickSort Description In this problem, you have to analyze a particular sorting ...
- HDU 4325 离散化+树状数组 或者 不使用树状数组
题意:给出一些花的开放时间段,然后询问某个时间点有几朵花正在开放. 由于ti<1e9,我们需要先将时间离散化,然后将时间点抽象为一个数组中的点,显然,我们需要进行区间更新和单点查询,可以考虑线段 ...
随机推荐
- 只有勇敢的人、鲁莽的人和绝望的人才会追求大的变革 – D.J. Anderson
只有勇敢的人.鲁莽的人和绝望的人才会追求大的变革 – D.J. Anderson http://www.cnblogs.com/lchrennew/p/Why-The-Future-Of-Agile- ...
- 关于清晰讲解linux正则表达式的博文分享
http://www.cnblogs.com/chengmo/archive/2010/10/10/1847287.html linux shell 正则表达式(BREs,EREs,PREs)差异比 ...
- Fast RCNN 学习
因为项目需要,之前没有接触过深度学习的东西,现在需要学习Fast RCNN这个方法. 一步步来,先跟着做,然后再学习理论 Fast RCNN 训练自己数据集 (1编译配置) Fast RCNN 训练自 ...
- Android EditText限制输入一些固定字符的属性
android:digits="abcdefghijklmnopqrstuvwxyz1234567890" 仅仅能输入这些
- Android 简单的代码混淆
Android的代码混淆是开发者需要了解的相关知识,它能够防止android应用程序的反编译.因为android程序多数是java语言开发的,而java代码很容易被反编译,所以为了使android应用 ...
- HTTP Status 404 - No result defined for action com.hebky.oa.classEntity.action.EntitysAction and result input
在开发中总遇到这个问题,No result defined for action:原因:Action中的属性值为空的时候,Struts2的默认拦截器会报错,但是又找不到input的Result,不能够 ...
- ARP网关占用
30网段已经发生了2次ARP了 排查方法:我直去核心交换机直连镜像口,用wireshark抓包,过滤出ARP的包 发现的确有ARP的攻击,因为没有统计公司电脑和无线路由的MAC地址,所以只能一个个把无 ...
- 修复CefSharp浏览器组件中文输入Bug
概述 最近在win10上开发wpf应用,需要将CefSharp中wpf版本的浏览器组件(版本号v51.0.0)嵌入到应用中,但是发现不支持中文输入,GitHub上有这个问题的描述,参照其提到的方法可以 ...
- zoj1025 Wooden Sticks
DAG转移,从切题的数量来看是一道水题,给你n个棒,大的可以延续小的,问最少上升子序列的个数. 其实这道题是用贪心来写的,因为这是个有向无环图,到达分叉口,每一条路都要便历,所以每条路应该一样对待,有 ...
- TCP/IP详解之:广播和多播
第12章 广播和多播 广播是将数据报发送到网络中的所有主机(通常是本地相连的网络): 多播是将数据报发送到网络的一个主机组: 这两个概念的基本点在于当收到送往上一个协议栈的数据帧时采用不同类型的过滤. ...