51nod 1376 最长递增子序列的数量(线段树)
51nod 1376 最长递增子序列的数量
第1行:1个数N,表示数组的长度。(1 <= N <= 50000)
第2 - N + 1行:每行1个数A[i],表示数组的元素(0 <= A[i] <= 10^9)
输出最长递增子序列的数量Mod 1000000007。
5
1
3
2
0
4
2
/*
51nod 1376 最长递增子序列的数量 problem:
给你一个数组,求其中最长递增子序列有多少个 solve:
对于第i个数a[i]而言,它需要知道已经出现的[1,a[i]-1]中最长递增子序列的长度以及数量. 所以可以利用线段树来维护,
然后利用长度和数量来更新a[i] hhh-2016/09/03-16:41:1
*/
#pragma comment(linker,"/STACK:124000000,124000000")
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <vector>
#include <math.h>
#include <queue>
#include <set>
#include <map>
#define lson i<<1
#define rson i<<1|1
#define ll long long
#define clr(a,b) memset(a,b,sizeof(a))
#define scanfi(a) scanf("%d",&a)
#define scanfs(a) scanf("%s",a)
#define scanfl(a) scanf("%I64d",&a)
#define scanfd(a) scanf("%lf",&a)
#define key_val ch[ch[root][1]][0]
#define eps 1e-7
#define inf 0x3f3f3f3f3f3f3f3f
using namespace std;
const ll mod = 1000000007;
const int maxn = 50010;
const double PI = acos(-1.0);
int a[maxn],t[maxn];
struct node
{
int l,r;
ll Max,num;
int mid;
} tree[maxn << 2]; void push_up(int i)
{
if(tree[lson].Max == tree[rson].Max)
{
tree[i].Max = tree[lson].Max;
tree[i].num = tree[lson].num + tree[rson].num;
tree[i].num %= mod;
}
else if(tree[lson].Max > tree[rson].Max)
{
tree[i].Max = tree[lson].Max;
tree[i].num = tree[lson].num;
}
else
{
tree[i].Max = tree[rson].Max;
tree[i].num = tree[rson].num;
}
} void build(int i,int l,int r)
{
tree[i].l = l,tree[i].r = r;
tree[i].Max = tree[i].num = 0;
tree[i].mid = (l+r) >> 1;
if(l == r)
return ;
build(lson,l,tree[i].mid);
build(rson,tree[i].mid + 1,r);
push_up(i);
} void push_down(int i)
{ } void update(int i,int k,ll len,ll many)
{
if(tree[i].l == tree[i].r && tree[i].l == k)
{
if(tree[i].Max < len) tree[i].Max = len,tree[i].num = many;
else if(tree[i].Max == len) tree[i].num += many;
tree[i].num %= mod;
return ;
}
int mid = tree[i].mid;
if(k <= mid)
update(lson,k,len,many);
else update(rson,k,len,many);
push_up(i);
}
ll tans,tnum;
void query(int i,int l,int r)
{
if(l > r)
{
tans = 0,tnum = 1;
return ;
}
if(tree[i].l >= l && tree[i].r <= r)
{
if(tans == -1)
tans = tree[i].Max,tnum = tree[i].num;
else
{
if(tans == tree[i].Max) tnum += tree[i].num;
else if(tans < tree[i].Max)
{
tans = tree[i].Max,tnum = tree[i].num;
}
tnum %= mod;
}
return ;
}
push_down(i);
int mid = tree[i].mid;
if(l <= mid)
query(lson,l,r);
if(r > mid)
query(rson,l,r);
return;
} int cnt = 0;
int main()
{
int n;
while(scanfi(n) != EOF)
{
for(int i = 0; i< n;i++){
scanfi(a[i]);
t[i] = a[i];
}
sort(a,a+n);
cnt = 0;
for(int i = 1;i < n;i++)
{
if(a[i] != a[cnt])
a[++cnt] = a[i];
}
// for(int i = 0;i <= cnt;i++)
// printf("%d\n",a[i]);
build(1,0,cnt);
ll tMax = 0,ans = 0;
int id = lower_bound(a,a+cnt+1,t[0]) -a;
update(1,id,1,1);
tMax = 1,ans = 1;
for(int i = 1;i < n;i++)
{
id = lower_bound(a,a+cnt+1,t[i]) -a;
// cout << id <<" " ;
tans = -1,tnum = 0;
query(1,0,id-1);
if(!tans && !tnum)
tnum = 1;
if(tMax < tans+ 1) tMax = tans+1,ans = tnum;
else if(tMax == tans+1) ans += tnum;
ans %= mod;
update(1,id,tans + 1,tnum);
}
// cout <<endl;
printf("%I64d\n",ans);
}
return 0;
}
51nod 1376 最长递增子序列的数量(线段树)的更多相关文章
- 51Nod 1376 最长递增子序列的数量 —— LIS、线段树
题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1376 1376 最长递增子序列的数量 基准时间限制:1 秒 空 ...
- 51NOD 1376 最长递增子序列的数量 [CDQ分治]
1376 最长递增子序列的数量 首先可以用线段树优化$DP$做,转移时取$0...a[i]$的最大$f$值 但我要练习$CDQ$ $LIS$是二维偏序问题,偏序关系是$i<j,\ a_i< ...
- 51nod 1376 最长递增子序列的数量(不是dp哦,线段树 + 思维)
题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1376 题解:显然这题暴力的方法很容易想到就是以每个数为结尾最 ...
- 51Nod 1376 最长递增子序列的数量 (DP+BIT)
题意:略. 析:dp[i] 表示以第 i 个数结尾的LIS的长度和数量,状态方程很好转移,先说长度 dp[i] = max { dp[j] + 1 | a[i] > a[j] && ...
- 51nod 1376 最长上升子序列的数量 | DP | vector怒刷存在感!
51nod 1376 最长上升子序列的数量 题解 我们设lis[i]为以位置i结尾的最长上升子序列长度,dp[i]为以位置i结尾的最长上升子序列数量. 显然,dp[i]要从前面的一些位置(设为位置j) ...
- 【51nod】1376 最长递增子序列的数量
数组A包含N个整数(可能包含相同的值).设S为A的子序列且S中的元素是递增的,则S为A的递增子序列.如果S的长度是所有递增子序列中最长的,则称S为A的最长递增子序列(LIS).A的LIS可能有很多个. ...
- 51nod-1134 最长递增子序列,用线段树将N^2的dp降到NlogN
题目链接 给出长度为N的数组,找出这个数组的最长递增子序列.(递增子序列是指,子序列的元素是递增的) 例如:5 1 6 8 2 4 5 10,最长递增子序列是1 2 4 5 10. Input 第1行 ...
- 51nod1376 最长递增子序列的数量
O(n2)显然超时.网上找的题解都是用奇怪的姿势写看不懂TAT.然后自己YY.要求a[i]之前最大的是多少且最大的有多少个.那么线段树维护两个值,一个是当前区间的最大值一个是当前区间最大值的数量那么我 ...
- 51nod 1134 最长递增子序列
题目链接:51nod 1134 最长递增子序列 #include<cstdio> #include<cstring> #include<algorithm> usi ...
随机推荐
- zookeeper 启动失败 BindException: Address already in use 或者Error contacting service. It is probably not running
平台:centos-6.3-i386 jdk-7u51 storm 0.9.1 python 2.6.6 hadoop 1.2.1 今天上午装storm的时候遇到这个问题,好郁闷.把网上介绍的方法 ...
- JAVA委托事件处理机制
1)事件:用户对程序的某一种功能性操作. Java中的事件主要有两种: 1.组件类事件 componentEvent.ContainerEvent.WindowEvent.FocusEvent.Pai ...
- sts 和 lombok
1.安装lombok.jar到sts.exe所在目录 如果是eclipse,需要放到eclipse.exe所在目录,同理myeclipse. 2.修改sts.ini配置使用lombok 如果是ecli ...
- 京东2019春招Java工程师编程题题解
生成回文串 题目描述 对于一个字符串,从前开始读和从后开始读是一样的,我们就称这个字符串是回文串. 例如"ABCBA","AA","A"是回 ...
- express学习(二)—— Post()类型和中间件
1.数据:GET.POST 2.中间件:使用.写.链式操作 GET-无需中间件 req.query POST-需要"body-parser" server.use(bodyPars ...
- 如何打开hprof文件
最近学习深入java虚拟机的书,照着里面的例子跑了下. 下面是demo: /** * VM Args:-Xms20m -Xmx20m -XX:+HeapDumpOnOutOfMemoryError * ...
- [SHOI2009] 会场预约 - Treap
Description PP大厦有一间空的礼堂,可以为企业或者单位提供会议场地.这些会议中的大多数都需要连续几天的时间(个别的可能只需要一天),不过场地只有一个,所以不同的会议的时间申请不能够冲突.也 ...
- Java中对List去重, Stream去重
问题 当下互联网技术成熟,越来越多的趋向去中心化.分布式.流计算,使得很多以前在数据库侧做的事情放到了Java端.今天有人问道,如果数据库字段没有索引,那么应该如何根据该字段去重?大家都一致认为用Ja ...
- 开源博客系统使用springmvc
https://github.com/Zephery/newblog http://www.wenzhihuai.com/index.html
- React-Native(三):React Native是基于React设计的
React Native是基于React js设计的. 参考:<React 入门实例教程> React 起源于 Facebook 的内部项目,因为该公司对市场上所有 JavaScript ...