HDU 2227-Find the nondecreasing subsequences(dp+BIT优化)
题意:
给你一个序列a[],求它的不降子序列的个数
分析:
dp[i]表示以i结尾不降子序列的个数,dp[i]=sum(dp[j])+1(j<i&&a[j]<=a[i]);答案就是sum(dp[i])
但发现一个问题n很大O(n^2)肯定超时,想起前面做的两道题,用线段树优化,它是用维护的是和,可以用BIT优化,但又发现a[i]的值很大没法存,就又想到了离散化,恩这个题就解决了。
#include <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <vector>
#include <string>
#include <cctype>
#include <complex>
#include <cassert>
#include <utility>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
typedef pair<int,int> PII;
typedef long long ll;
#define lson l,m,rt<<1
#define pi acos(-1.0)
#define rson m+1,r,rt<<11
#define All 1,N,1
#define N 100010
#define read freopen("in.txt", "r", stdin)
const ll INFll = 0x3f3f3f3f3f3f3f3fLL;
const int INF= 0x7ffffff;
const int mod = ;
ll a[N],bit[N],tmp[N],dp[N];
int n;
void add(int x,ll d){
while(x<=n){
bit[x]=(bit[x]+d)%mod;
x+=(x&(-x));
}
}
ll sum(int x){
ll num=;
while(x>){
num=(num+bit[x])%mod;
x-=(x&(-x));
}
return num;
}
int main()
{
while(~scanf("%d",&n)){
for(int i=;i<=n;++i){
scanf("%I64d",&a[i]);
tmp[i]=a[i];
}
sort(tmp+,tmp+n+);
int len=unique(tmp+,tmp+n+)-tmp-;
/*for(int i=1;i<=len;++i)
cout<<tmp[i]<<endl;*/
memset(dp,,sizeof(dp));
memset(bit,,sizeof(bit));
ll total=;
for(int i=;i<=n;++i){
int pos=lower_bound(tmp+,tmp+len+,a[i])-tmp;
dp[i]=;
dp[i]=(dp[i]+sum(pos))%mod;
add(pos,dp[i]);
total=(total+dp[i])%mod;
}
printf("%I64d\n",total);
}
return ;
}
HDU 2227-Find the nondecreasing subsequences(dp+BIT优化)的更多相关文章
- 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 dp思想 + 树状数组
http://acm.hdu.edu.cn/showproblem.php?pid=2227 用dp[i]表示以第i个数为结尾的nondecreasing串有多少个. 那么对于每个a[i] 要去找 & ...
- 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 (数状数组)
Find the nondecreasing subsequences Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/3 ...
- HDU 2227 Find the nondecreasing subsequences
题目大意:给定一个序列,求出其所有的上升子序列. 题解:一开始我以为是动态规划,后来发现离散后树状数组很好做,首先,c保存的是第i位上升子系列有几个,那么树状数组的sum就直接是现在的答案了,不过更新 ...
- hdu 5745 La Vie en rose DP + bitset优化
http://acm.hdu.edu.cn/showproblem.php?pid=5745 这题好劲爆啊.dp容易想,但是要bitset优化,就想不到了. 先放一个tle的dp.复杂度O(n * m ...
- HDU 3506 (环形石子合并)区间dp+四边形优化
Monkey Party Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)Tot ...
- HDU 5313——Bipartite Graph——————【二分图+dp+bitset优化】
Bipartite Graph Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)T ...
- hdu 2227(树状数组+dp)
Find the nondecreasing subsequences Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/3 ...
随机推荐
- 阻止事件冒泡(stopPropagation和cancelBubble)和阻止默认行为(preventDefault和returnValue)
<div id="divId1" style="width:500px;height:500px;background-color:#3ac;text-align: ...
- 第一个java程序(hdu 1001)
//package yy;不能有 import java.util.*; public class Main {//必须为Main public static void main(String[] a ...
- 第三章:推荐系统冷启动与CB
3.1冷启动问题简介: 冷启动问题(cold start)主要分三类: • 用户冷启动 • 物品冷启动 • 系统冷启动 参考解决方案: • 推热门 • 利用用户 ...
- POJ 2114 Boatherds【Tree,点分治】
求一棵树上是否存在路径长度为K的点对. POJ 1714求得是路径权值<=K的路径条数,这题只需要更改一下统计路径条数的函数即可,如果最终的路径条数大于零,则说明存在这样的路径. 刚开始我以为只 ...
- PHP Redis 普通封装类
class redisInit { private $redis; //redis对象 /** * 初始化Redis * $config = array( * 'server' => '127. ...
- php扩展函数调用扩展中的标准函数
这几天在写php的扩展函数,在网上学习步骤什么的都有,一般问题也都能查到,所以就不再此啰嗦,写这篇博客的原因是因为遇到的一个问题,百度谷歌都没找到,对于初学者,这个或许有用,对于过来人,我想他们肯定也 ...
- 面向对象设计Object Oriented Design
http://www.codeproject.com/Articles/93369/How-I-explained-OOD-to-my-wife http://www.cnblogs.com/niyw ...
- Linux改IP后务必重启网络服务
[root@localhost ~]# vi /etc/sysconfig/network-scripts/ifcfg-eth0 DEVICE=eth0BOOTPROTO=noneONBOOT=yes ...
- 面试题_82_to_87_Date、Time 及 Calendar 的面试题
82)在多线程环境下,SimpleDateFormat 是线程安全的吗?(答案)不是,非常不幸,DateFormat 的所有实现,包括 SimpleDateFormat 都不是线程安全的,因此你不应该 ...
- 定制IE浏览器的尖兵利器 - BHO
IE浏览器是当前使用人数最广的浏览器, 本文主要来讲述如何来打造我们自己特色的浏览器, 自定义工具栏按钮, 自定义网页的右击菜单, BHO技术与IE浏览器. 本文写作过程中参考不少网络上的相关资料, ...